note 107894 added to function.strtotime

From: Date: Tue, 13 Mar 2012 15:07:57 +0000
Subject: note 107894 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-186631@lists.php.net to get a copy of this message
I need to calculate the number of days between two given dates using strtotime(), and I found that if March 12 is in between two dates, the result is incorrect. $date1 = '3/9/2012'; for ($i = 10; $i <= 15; $i++){ $date2 = '3/' . $i . '/2012'; $date1PHP = strtotime($date1); $date2PHP = strtotime($date2); $diff = $date2PHP - $date1PHP; $diffDay = $diff/(60*60*24); echo "<p>[<strong>{$date2}</strong>]: {$diff} seconds ({$diffDay} days)</p>"; } This will return: [3/10/2012]: 86400 seconds (1 days) [3/11/2012]: 172800 seconds (2 days) [3/12/2012]: 255600 seconds (2.9583333333333 days) [3/13/2012]: 342000 seconds (3.9583333333333 days) [3/14/2012]: 428400 seconds (4.9583333333333 days) [3/15/2012]: 514800 seconds (5.9583333333333 days) As you can see, the result starts looking weird after March 12. To solve this problem, simply call the following code before running strtotime: date_default_timezone_set('UTC'); That will solve the problem. ---- Server IP: 69.147.83.197 Probable Submitter: 75.134.20.40 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- https://master.php.net/note/edit/107894 Del: integrated -- https://master.php.net/note/delete/107894/integrated Del: useless -- https://master.php.net/note/delete/107894/useless Del: bad code -- https://master.php.net/note/delete/107894/bad+code Del: spam -- https://master.php.net/note/delete/107894/spam Del: non-english -- https://master.php.net/note/delete/107894/non-english Del: in docs -- https://master.php.net/note/delete/107894/in+docs Del: other reasons-- https://master.php.net/note/delete/107894 Reject -- https://master.php.net/note/reject/107894 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186631) next »