Jump to content

How to extract date from UTC?


Go to solution Solved by Psycho,

Recommended Posts

  • Solution

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.

Edited by Psycho

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".

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

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.