0

I have a date format like this 'Tue May 01 00:00:00 +1000 2012' (from array data on json file)

when I use date() function it returning April, :D

      echo date('F Y', strtotime('Tue May 01 00:00:00 +1000 2012'));

      //it returning "April 2012"

Any ideas how to fix this?

Many Thanks!

3
  • What's your aim? How It should appear then? Commented Apr 26, 2013 at 23:23
  • 3
    Notice the timezone specification on the provided date/time? When it's May 1st over there, it's still April 31 on your own timezone. How to fix it? You can simply remove the +1000 to get the result you want, but I 'm not sure if that's fixing or breaking it. Commented Apr 26, 2013 at 23:24
  • There is no such date as April 31, Jon. Commented Jun 6, 2014 at 0:49

2 Answers 2

2

Instead of using the old date/time functions that mess things up because they implicitly involve your local time zone, use DateTime:

$date = new DateTime('Tue May 01 00:00:00 +1000 2012');
echo $date->format('F Y');

This will also work corrrectly for any date, regardless of the timezone (UTC+10 hours or anything else).

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

Comments

2

You will need to check both the timezones. Or set a custom one for both date and strtotime calls.


Since you are using +1000 as your timezone-offset; I'm assuming it is Australia. You can use the date_default_timezone_set() call to set timezone to Australia.

echo date('F Y', strtotime('Tue May 01 00:00:00 +1000 2012'));
date_default_timezone_set('Australia/Queensland');
echo date('F Y', strtotime('Tue May 01 00:00:00 +1000 2012'));

Here is the codepad link: http://codepad.org/tPC8DEQp

4 Comments

Irrelevant. The timezone is specified inline in the input.
@hjpotter92: This will only work for dates where the TZ specification is +1000 -- in which case, why (globally) change the timezone instead of (locally) removing the +1000 from the string? Also, Queensland does not observe DST right now, but it could do so in the future (as it has in the past) and if that happens then you have a bug. So even if you wanted to change the TZ, it would be better to change it to Etc/GMT+10.
@Jon Using Etc/GMT-10 seems to solve the problem now: codepad.org/kxQj2sEO
@hjpotter92: Right, -10 is correct. But all of these solutions are less than satisfactory. I have added an answer of my own.

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.