2

I have an issue with converting time string I get from JSON to another format. Somehow the date is set to minus 24 hours. Here's object from JSON

 [date] => 2011-07-02T00:00:00+02:00

I'm using strtotime() and date()

date('l, d F Y', strtotime($day->date));

But the output looks like this

FRIDAY, 01 JULY 2011

Obviously the date in JSON is Second of July. Does anyone have any idea why this happens? Am I missing something important? Will really appreciate any help!

0

2 Answers 2

4

I think you should use DateTime. It does not depend on hosts TimeZone. Beside the format is valid ISO8601. So DateTime would have not problem at all.

$dt = new DateTime("2011-07-02T00:00:00+02:00");
echo $dt->format("l, d F Y"); 

// Echos Saturday, 02 July 2011

http://ideone.com/yPp4d

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I fixed it by calling date_default_timezone_set
@JuliaDemchenko Thats a bad practice to alter default time zone that affects whole application
I understand. In my case the time is bound to an event which takes place in a certain timezone, so there is no need to show time depending on the user timezone.
If have to parse different timezones for different users, you'll face problem!
-1

PHP doesn't understand an infinite array of time/date strings. What is 'obvious' to a human, is not so obvious to a computer. Without a specific parser for that exact date format, how is the computer language to understand what the T in your example is for??

PHP strtotime formats will show you what formats PHP can convert a string from, to a time or date object.

Even as the date/time is parsing correctly, your tzcorrection of +0200 is telling PHP to correct for a timezone difference of GMT + 2 hours, which is likely not your correct timezone offset and thus giving you the error.

1 Comment

-1 This is the well-known ISO8601 format, which strotime definitely understands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.