Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP strtotime() function wrong by 1 hour?

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?

like image 249
David Avatar asked Jul 21 '10 21:07

David


People also ask

Why is Strtotime return false?

If the date string isn't understood by strtotime() then it will return false. When this happens you can try a few things to force strtotime() to parse the date correctly. Sometimes it's something as simple as swapping the slashes for dashes, which forces strtotime() to parse the date in a different way.

How do you add hours on Strtotime?

PHP strtotime() Function "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") .

Which is a valid Strtotime () function in PHP?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.

How do I change Strtotime?

Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );


1 Answers

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.

like image 87
Yahel Avatar answered Oct 13 '22 22:10

Yahel