Jump to content

How to extract date from UTC?


16vMiniMike

Recommended Posts

Where are you getting the value '2015-11-17T00:00:00+00:00' from? That's not the format I would expect from a DB query or a timestamp generated from PHP.

 

Anyway, give this a try

//Create new datetime object as a UTC time value
$date = new DateTime('2015-11-17T00:00:00+00:00', new DateTimeZone('UTC'));
//Set the timezone for the output and format it
$dateOuptut = $date->setTimezone(new DateTimeZone('Europe/London'))->format('j M Y');
//Output the value
echo $dateOuptut;

NOTE: I added a time to the output to test and for "America/Chicago" it showed the value that I would expect (UTC - 6 hours), but from what I found London should be UTC + 1 and I was not getting those results. You could always artificially add an hour to the input time.

Link to comment
Share on other sites

but from what I found London should be UTC + 1 and I was not getting those results.

Europe/London is UTC+1 during daylight savings, which ended 2015-10-25. It's UTC+0 normally. Either way the result would be "17 Nov 2015".
Link to comment
Share on other sites

Europe/London is UTC+1 during daylight savings, which ended 2015-10-25. It's UTC+0 normally. Either way the result would be "17 Nov 2015".

 

I see. But, I testing with some other dates with +/- 30 minutes to see if the passed date (UTC) and the displayed date would be different. I must not have used a correct value to see an instance where they would be different for that one hour period. If the passed timestamp will always be 00:00:00 it is a non issue. But, if it varies then I assume the displayed date should be one day ahead if the time is 11PM to midnight

Link to comment
Share on other sites

You'll get a different date with timezones west of the input (so UTCDemo

 

If it's just about the date then just leave the DateTime with the timezone given (so no setTimezone) and format from there. Even if changing the time was an option, it's not a good idea to get into that habit because some timezones are beyond UTC+/-12 so there will always be someone on a different date.

Link to comment
Share on other sites

Excellent thanks guys, yes Psycho's code worked a treat  :):)

Just as a little background, the UTC date was being returned by an API as a string and I've been scratching my head trying to extract just the date.

Many thanks for the solution, saved me hours!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.