note 11650 deleted from function.strtotime by jimw
| From: | jimw@php.net | Date: | Fri, 14 Dec 2001 20:56:56 +0000 |
| Subject: | note 11650 deleted from function.strtotime by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22795@lists.php.net to get a copy of this message | ||
I needed to use strtotime() to parse a simple "MM/DD/YYYY" string. Using the same code on
three different machines yielded three different results. As it turns out each of the machines was
in a different time zone.
$month = "02";
$day = "28";
$year = "2001";
//date_string = "02/28/2001"
$date_string = "" . $month . "/" . $day . "/" . $year;
$date_converted_string = strtotime($date_string);
$date_check_string = date("m/d/Y H:i:s T",$date_converted_string);
*****
date_check_string would not consistently equal date_string until I did this:
*****
$local_timezone = date("T");
if (preg_match("/EST/",$local_timezone)) {
$timezone_str = "EDT";
} else if (preg_match("/CST/",$local_timezone)) {
$timezone_str = "CDT";
} else if (preg_match("/PST/",$local_timezone)) {
$timezone_str = "PDT";
} else if (preg_match("/MST/",$local_timezone)) {
$timezone_str = "MDT";
}
$month = "02";
$day = "28";
$year = "2001";
//date_string= "02/28/2001 23:59:59 <timezone-of-this-machine>"
$date_string = "" . $month . "/" . $day . "/" . $year . "
23:59:59 " . timezone_str;
$date_converted_string = strtotime($date_string);
$date_check_string = date("m/d/Y H:i:s T",$date_converted_string);
*****
Now, $date_check_string equals $date_string regardless of the time zone of the machine I have this
code running on.
Note that I added a time of 23:59:59 to the string value to make this work. If you remove the time
from the string, this time zone code above is irrelevant. It wasn't until I added the time to
the date string that I found out more specifically what the strtotime() function was adding or
subtracting from the date string I was trying to convert. Sometimes strtotime() even returned the
correct date but only when I added the time to the string did I find out that sometimes it was
adding or subtracting say 12 hours from my date string. If you remove the time from the string, this
time zone code above becomes irrelevant.
I should also note that I found that changing the values of timezone_str above to EST instead of EDT
and CST instead of CDT and so on makes date_check_string unequal to date_string again.