16vMiniMike Posted April 3, 2017 Share Posted April 3, 2017 Hi, newbie here I have a string (2015-11-17T00:00:00+00:00) but all i need is the date in UK format, so '17 Mar 2015' in this example.I'm really struggling to find how to do this, can anyone help?!Thanks, Mike Quote Link to comment https://forums.phpfreaks.com/topic/303611-how-to-extract-date-from-utc/ Share on other sites More sharing options...
Solution Psycho Posted April 3, 2017 Solution Share Posted April 3, 2017 (edited) 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 April 3, 2017 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/303611-how-to-extract-date-from-utc/#findComment-1544991 Share on other sites More sharing options...
requinix Posted April 3, 2017 Share Posted April 3, 2017 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". Quote Link to comment https://forums.phpfreaks.com/topic/303611-how-to-extract-date-from-utc/#findComment-1544994 Share on other sites More sharing options...
Psycho Posted April 3, 2017 Share Posted April 3, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303611-how-to-extract-date-from-utc/#findComment-1544999 Share on other sites More sharing options...
requinix Posted April 3, 2017 Share Posted April 3, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303611-how-to-extract-date-from-utc/#findComment-1545000 Share on other sites More sharing options...
16vMiniMike Posted April 4, 2017 Author Share Posted April 4, 2017 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! Quote Link to comment https://forums.phpfreaks.com/topic/303611-how-to-extract-date-from-utc/#findComment-1545009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.