note 107894 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Tue, 13 Mar 2012 17:23:20 +0000 |
| Subject: | note 107894 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186645@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.
<?php
$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:
<?php date_default_timezone_set(\'UTC\'); ?>
That will solve the problem.
--was--
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.
http://php.net/manual/en/function.strtotime.php