I have date in format "m-Y-d". How can i modify this date to format "Y-d-m"? For this the best should be function where i can add old format and new format, but how can i make it?
For example i have
$date = '01-2013-13'; // "m-Y-d"
i would like receive:
$newDate = '2013-13-01'; // "Y-d-m"
See DateTime::createFromFormat() and DateTime::format()
$date = DateTime::createFromFormat('m-Y-d', '01-2013-13');
echo $date->format('Y-m-d');
PHP cannot parse the format using strtotime(). You'll have to do something like this:
function reformat($old_date)
{
$parts = explode('-', $old_date);
return $parts[1].'-'.$parts[2].'-'.$parts[0];
}
And then call it using:
$new_format = reformat($date);
Alternatively, you can use the DateTime::createFromFormat():
function reformat($old_date)
{
$new_date = DateTime::createFromFormat('m-Y-d', $old_date);
return $new_date->format('Y-m-d');
}
You can use the DateTime class, for example:
$date = new DateTime('01-2013-13');
Then use the format() method, like:
$date->format('Y-m-d');
More info:
http://www.php.net/manual/en/datetime.format.php
To create you need to use:
$newDate = date('Y-d-m');
You can find more information here!
Related
I have this format:
How do I use PHP DateTime to format like 2023-01-27T17:37:00.000Z
So far I have
$date = new DateTime();
$string = $date->format('Y-m-dTHH:H:i:s');
but it outputs 2022-11-25UTC0000:00:00:00
What is the correct format
Is there a ressource on the web that would find it for me ? like a helper website.
You can do as follows:
$date = new DateTime();
$string = $date->format('Y-m-d\TH:i:s.000\Z');
echo $string;
You can use the DateTime class to format the date like this:
$date = new DateTime('2023-01-27T17:37:00.000Z');
echo $date->format('Y-m-d\TH:i:s.u\Z'); // Outputs 2023-01-27T17:37:00.000Z
I have to convert a string input to date "1990/07/22" to 22/07/1990 as date,My function is as follows as:
public function($date){
$date_format_sec = strtotime($date);
$date_of_birth = new \DateTime('#'.$date_format_sec);
}
It uploads date as 21/07/1990 since I didn't give time.How to get the exact date given as same as input.
You can format your date with php
$formatedDate = $date_of_birth->format('d-m-Y');
Documentation
$input = '1990/07/22';
$date = \DateTime::createFromFormat('Y/m/d', $input)->format('d-m-Y');
As I said, you don't need to use strtotime, if the $date string is a valid date, the class DateTime will read it. After that you can use format.
In this example I set the format to be always the one you expect, while you can put any other format accepted by it.
public function getMyDate($date, $format = 'd/m/Y') {
$date_of_birth = new \DateTime($date);
return $date_of_birth->format($format);
}
public function formatDate($date) {return date('d/m/Y', strtotime($date));}
I tried the following code to change the dateformat from dmy to ymd, but when using i got wrong dates.
My code
$sdate11=date("Y-m-d", strtotime($_POST["txtstartdates"]) );
$sdate111=date("Y-m-d", strtotime($_POST["txtenddates"]) );
dates inserted were
30-05-2013 and 31-05-2013
the date it returned was
2035-11-03 and 2036-11-02
could you please help me to find what was the problem here and solve it
Thank you.
Try with split like
$a = split('-',$_POST["txtstartdates"]);
or you can use explode even like
$a = explode('-',$_POST["txtstartdates"]);
$my_new_date = $a[2].'-'.$a[1].'-'.$a[0];
Here strtotime will not work for the format dd-mm-yyyy
You might use DateTime for that:
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtstartdates']);
echo $date->format('Y-m-d');
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtenddates']);
echo $date->format('Y-m-d');
Can't you use the DateTime object to convert the date into the format you want?
$DateTime = new DateTime($_POST['FIELD']);
echo $DateTime->format('Y-m-d');
Your code seems correct , I dont know why its not working for you Can you try below
I have made the '' from "" only
$sdate11=date('Y-m-d', strtotime($_POST['txtstartdates']));
$sdate111=date('Y-m-d', strtotime($_POST['txtenddates']));
You can do it using single line as:
$show_date = DateTime::createFromFormat('d-m-Y', $dateInput)->format('Y-m-d');
I've some data which formatted like: 04.09.1953
I want to convert this format to: 1953-09-04
Is there any way or php function to do this?
just use strtotime() to get a timestamp and then date() to convert that timestamp to the format you need:
$timestamp = strtotime("04.09.1953");
echo date("Y-m-d", $timestamp);
EDIT:
If you're having some "exotic" format as input, you might need to use explode(), list() and mktime() to build the timestamp on your own:
list($y,$m,$d) = explode(".","04.09.1953");
$timestamp = mktime(0,0,0,$m,$d,$y);
echo date("Y-m-d", $timestamp);
Have you tried strtotime() ? It might work, else you'll need to do manual conversion using substrings or explodes.
http://php.net/strtotime
http://php.net/substring
http://php.net/explode
If all you are interested in is converting from one "string" format to another you can use a regEx:
$DMY = '04.09.1953';
$YMD = preg_replace('/(\d\d).(\d\d).(\d{4,4})/', '$3-$2-$1', $DMY);
http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/reformat_date.html
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
I'm using functions with the datetime object that require a datetimezone object as an argument. I see a list of timezones here:
http://www.php.net/manual/en/class.datetimezone.php
but there's not things like 'est'. How could I create a 'datetimezone' object from EST?
$tz = new DateTimeZone('EST');
In http://www.php.net/manual/en/timezones.php you can find in the OTHER section the EST there.
To create one use date_default_timezone_set('EST');
To make sure you have it do echo date_default_timezone_get();
A very verbose loop. The construct function for the DateTime class isn't working properly for me but this works.
$date = "2011/03/20";
$date = explode("/", $date);
$time = "07:16:17";
$time = explode(":", $time);
$tz_string = "America/Los_Angeles"; // Use one from list of TZ names http://us2.php.net/manual/en/timezones.php
$tz_object = new DateTimeZone($tz_string);
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
$datetime->setDate($date[0], $date[1], $date[2]);
$datetime->setTime($time[0], $time[1], $time[2]);
print $datetime->format('Y/m/d H:i:s');
?>
Be aware of that some TimeZone abbreviations like EST are not unique. See http://www.timeanddate.com/library/abbreviations/timezones/.
The usage of EST is not recommended, see
http://www.php.net/manual/en/timezones.others.php.
Use for example America/New_York instead.