PHP convert date and time from UTC to Europe/London [duplicate] - php

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');

Related

Convert Time to ISO 8601 format in UTC timezone

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

PHP Convert From UTC to Local Datetime format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Need a little help.
I'm using PHP.
Here is a example date:
Mon Sep 05 2016 15:30:00 GMT+0800 (China Standard Time)
What i want is display a outpout like this:
2016-09-05 15:30:00
I've tried to convert it using this function:
date('Y-m-d h:i:s', strtotime($time));
But the output is:
1970-01-01 08:00:00
Need some advice
Thanks.
Just use DateTime():
$time = 'Mon Sep 05 2016 15:30:00 GMT+0800';
$date = new DateTime($time);
echo $date->format('Y-m-d H:i:s');
<?php
$timezone = -5; //(GMT -5:00) EST (U.S. & Canada)
echo gmdate("Y-m-j H:i:s", time() + 3600*($timezone+date("I")));
?>
This doc would help more
http://php.net/manual/en/function.gmdate.php
$date=date_create("Mon Sep 05 2016 15:30:00 GMT+0800");
echo date_format($date,"Y/m/d H:i:s");

Time zone confusion processing javascript generated date in php

I am located in the (PDT) time zone at time Sat May 11 2013 20:58:51 (my time) I generated a date/time using the following code.
var date = new Date();
alert(date);
This returns the result
"Sat May 11 2013 20:58:51 GMT-0700 (PDT)"
If I then post this date to a php script which processes it in the following way:
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
I get the result
"2013-05-12 03:58:51"
Not what I expected. I only get the expected time if I get rid of the "GMT-0700 (PDT)" part from my date/time. So I have two questions.
Can anyone tell me how to generate a date of this format but with out the "GMT-0700 (PDT)" part in javascript without using string functions/regex?
Is my browser giving me the wrong time zone, or is php interpreting the time zone incorrectly. In either case, why?
JavaScript always take your local server timezone, while PHP is converting your date to UTC, so you need to set your server time zone before using strtotime as follow,
date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
DEMO.
Or if you would like to convert your js date to UTC than use,
var date = new Date();
var utcdate = date.toUTCString()
$date = date('r', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
1- echo date("l F j, Y, H:i s");
2-
date_default_timezone_set('America/Los_Angeles');
$postedDate = "Sat May 11 2013 20:36:24 GMT-0700 (PDT)";
$date = date('Y-m-d H:i:s ', strtotime($postedDate));
echo json_encode($date);

convert into a DateTime object

Is there a way to convert this:
Tue Aug 28 13:59:01 +0000 2012
Into a DateTime Object?
I need to diff the above value with a DateTime Object.
Just pass it into the DateTime constructor...
$date = new DateTime('Tue Aug 28 13:59:01 +0000 2012');
echo $date->format('Y-m-d H:i:s');
$date = new DateTime($yourDate);
this will covert to standard DateTime object
check php manual here . Example 2 will make it clear.

Linux timestamp to PHP

I have the following timestamp:
1342259667654
which when converted with http://www.epochconverter.com/ gives:
Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2
And that is the correct time, but when using:
echo date("Y-m-d H:i:s", 1342259667654);
I get the following date:
1904-07-24 10:22:47
How can I get with PHP the exact date out of this time stamp?
Your timestamp needs to be divided by 1000:
echo date("Y-m-d H:i:s", 1342259667654/1000);
$timestamp = 1342259667;
$dt = new DateTime("#$timestamp"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
You can also do it this way.
The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.

Categories