I have date as Fri, 15 Mar 2019 08:56:57 +0000
I want to convert this date in ISO 8601 format in UTC timezone in php.
I have tried the following:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$output = $date->format(\DateTime::ATOM);
Is this correct?
If you want the ISO 8601 format you should use the DateTimeInterface::ISO8601 in the format method, or you can use "c":
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
echo $date->format(\DateTime::ISO8601);
// will be 2019-03-15T08:56:57+0000
echo $date->format("c");
/*
will be 2019-03-15T08:56:57+00:00
note the : in between hh and mm in offset(timezone)
generally accepted as valid ISO 8061 see:
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*/
Regarding the timezone if you want to force it into UCT timezone then you should use the setTimezone method on the date object first with timezone param "UTC":
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$date->setTimezone(new DateTimeZone("UTC"));
$output = $date->format(\DateTime::ISO8601);
Note about the above that if your original date time is not in UTC(has an offset) the time will be converted to UTC and the offset will be 0000:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0230");
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T08:56:57+0230
$date->setTimezone(new DateTimeZone("UTC"));
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T06:26:57+0000
Related
My vuexy datepicker returns the following string:
Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)
In my controller (Laravel) I want to convert this into simple date format - yyyy-mm-dd.
I have tried with strtotime() & date() but nothing works. It may need to use DateTime::createFromFormat() but I could not pick the syntax.
How can I convert it into yyyy-mm-dd?
This is working for me:
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date); # Get rid of TZ from the date string
echo date('Y-m-d', strtotime($date));
Here's another sample using the DateTime class
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date);
$dt = new DateTime($date);
echo $dt->format('Y-m-d');
Both Output:
2020-12-01
Hi Friend You Can Use Carbon Package one of the most recomonded package. Please follow the code as below
use Carbon; //use at the begining of class/controller
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$formated_date = Carbon::parse($date)->format('Y-m-d');
//output will be 2020-12-01
This question already has answers here:
Convert time and date from one time zone to another in PHP
(6 answers)
Closed 7 years ago.
I have the following date string in the following format:
$received = "Tue, 15 Sep 2015 12:35:03 +0000 (UTC)";
I would like to convert this to the Europe/London timezone as the actual time is supposed to read 13:35:03
Any ideas how this can be done?
Thanks
Set a new Timezone maybe you want to set this dynamically then set the new dateTime from database:
$received = "Tue, 15 Sep 2015 12:35:03 +0000 (UTC)";
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($received);
$date->setTimezone($tz);
echo $date->format('H:i:s');
The output is:
13:35:03
The current/correct way to do this:
$received = "Tue, 15 Sep 2015 12:35:03 +0000 (UTC)";
$date = new DateTime($received);
echo $date->format('c'); // 2015-09-15T12:35:03+00:00
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('c'); // 2015-09-15T13:35:03+01:00
You build the DateTime object from the string, then change the timezone.
hope this helps you need to create DateTime object then convert the time zone
when you display after you change the time zone then it will display correct time.
$t = new DateTime($received);
date_timezone_set($t, timezone_open('Europe/London'));
echo date_format($t, 'Y-m-d H:i:sP');
how i can change this Date : Tue Oct 29 20:36:21 +1100 2013 to proper format (Y-m-d H:i:s) in php?
How about this?
$time = "Tue Oct 29 20:36:21 +1100 2013";
echo date ("Y-m-d H:i:s", strtotime($time));
This will print time in your current time zone.
I am getting a date format like Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time) form a javascript to my php function.
I want to store that to my database table in "2013-04-22 12:16:00" format.
Can any one help me to convert this date type.
I tried:
date("Y-m-d H:i:s",$startDate);
But it's giving error as
date() expects parameter 2 to be long string given
Use strtotime() and date():
$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
or use DateTime:
<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>
DEMO
Try using strtotime to convert the timestamp to unix-format so you can use it in the date() function:
date("Y-m-d H:i:s",strtotime($startDate));
You can also try using the (usually included) DateTime class. In particular, have a look at DateTime::createFromFormat. It may help you get around ambiguities in the date string (strtotime() will sometimes fail or mis-parse a date string). DateTime::createFromFormat allows you to specifically designate the format of the date string so there can be no ambiguity.
firstly you need to save it as Timestamp so,
$startDate = strtotime($javascript_value);
$result = date("Y-m-d H:i:s",$startDate);
I'm receiving a string containing date and time (in UTC format) for when something was created. The string looks like this: "Wed Mar 13 14:10:20 +0000 2013". Now, I need to convert that to a more readable format. Something like this "14:10, 13 Mar" or preferably "1 hour ago", "1 week ago" etc.
How do I do this?
Thanks.
Use the DateTime object. The constructor should be able to parse that timestamp, then you can mainpulate the date and output in any format you wish:
$date = new dateTime('Wed Mar 13 14:10:20 +0000 2013');
echo $date->format('h:i, d-M');
For relative times, see: Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon"
Try this:
$dt = 'Wed Mar 13 14:10:20 +0000 2013';
echo date( 'h:i, d-M', strtotime( $dt ) );
The time difference will require more code.