16

I am converting dates and times into timestamps using PHP before they are inserted into my MySQL database.

My problem is that when i use PHP's strtotime function the output timestamp is -1 hour behind my actual time.

For example, considering todays date: 07/21/2010. When i use php code like:

<?php
$my_timestamp = strtotime("07/21/2010");
echo $my_timestamp;
?>

The timestamp sent back is the GMT equivilent MINUS 1 hour. i.e. i get back: 20 Jul 2010 23:00:00 GMT instead of 21 Jul 2010 00:00:00 GMT.

I live in the UK so my timezone is GMT. I have declared my timezone in the script using date_default_timezone_set('Europe/London') and i have also ensured that the php.ini file is set to 'Europe/London'.

Is this something to do with daylight savings time perhaps? How can i fix the problem without adding 1 hour to all my dates?

4
  • If you just need to add 1 hour to the dates, add 3600 to the timestamp (3600 seconds = 1 hour) Commented Jul 21, 2010 at 21:24
  • 13
    Your timezone is not GMT, UK is on summer time now which is GMT +1 Commented Jul 21, 2010 at 21:25
  • 2
    @nos Why not add that as an answer, I think it's the correct one. Commented Jul 21, 2010 at 21:26
  • 1
    just to clarify, GMT as the time zone still exists even when us in the UK aren't on it. Our time zone right now is BST (GMT+1) so your faulty assumption that you're on GMT is the cause of your issue as has been pointed out. No worse than the guy in California who told me he was always on PDT all year round :( Commented Jul 21, 2010 at 21:29

3 Answers 3

29

Europe/London time is not GMT time during Daylight Savings. You need to set it to UTC.

date_default_timezone_set('UTC');

date_default_timezone_set('GMT'); may work, but, as Kenneth notes in a comment below, it is deprecated.

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

1 Comment

CAUTION: GMT has since been depreciated in more recent versions of PHP and while it is still being supported temporarily, it is strongly recommended that UTC is used instead.
1

London time zone is expressed in British Summer Time during Summer. Having said that, it's a good practice to store time in UTC and present the time to end user in either UTC or in THEIR local time.

It is also probably wise to ensure that your system time is UTC.

Comments

-9

Before PHP 5.3 the strtotime had problems calculating time if you added or deducted months and days etc. That was fixed so you now can tell specifically how you want it calculated, if you are below PHP 5.3 I would recommend doing the date calculating in mysql or upgrade your PHP version.

18 Comments

Sure it isn't your understanding the PHP's date and time functions that is "bugged"?
@Android Noob I'm not sure I'd want that to work. 1 month is not a fixed unit of measurement. It could be 28, 29, 30 or 31 days depending on the month, unlike the other strtotime() interpretations that do work (+1 day, +1 week), which are always a fixed quantity of time, regardless of when they occur. (That said, I'm not one of your down-voters).
So it is as I expected. +1 month simply increases the month part with 1. Because 2009-02-31 doesn't exist it overflows to 03. This is consistent with the GNU specifications. You can use first day of next month or last day of next month.
@yc That's because 2008 is a leap year. So 2008-02-31 overflows to 2008-03-02 instead.
@Android How is it "not consistent?" +1 month adds 1 month to the month part of the date. If that date does not exist, it overflows. It's being doing this consistently since the function was created. Just because it doesn't fit your concept idea of what it should do, it doesn't mean it's "bugged." The function behaves consistently and correctly. In fact, Jan-31 + 1 month = Mar 2nd makes total sense. What did you expect it to do?
|

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.