문제

I'm reading a Last-Modified header which as a string is "Mon, 21 May 2013 09:10:30 GMT" and trying to compare that to my local time() (New Zealand). But I've just noticed that strtotime is making the date the "27" instead of "21" when "Mon, " is included in the string. Is that normal? Am I doing something wrong? Think I'm missing something...

$strtotime = strtotime("Mon, 21 May 2013 09:10:30 GMT");
$strtotime_date = date("Y-m-d H:i:s",$strtotime);

//[strtotime] => 1369645830
//[strtotime_date] => 2013-05-27 21:10:30
도움이 되었습니까?

해결책

  1. The reason for the error is that there is nothing like Mon, 21 May 2013 from my calendar 21st May is Tuesday

  2. From PHP DOC

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

Examples

$strtotime = strtotime("Tue, 21 May 2013 09:10:30 GMT");
echo date("Y-m-d H:i:s", $strtotime),PHP_EOL;

$strtotime = DateTime::createFromFormat("D, d M Y g:i:s O", "Tue, 21 May 2013 09:10:30 GMT");
echo $strtotime->format("Y-m-d H:i:s");

Output

2013-05-21 11:10:30  <- strtotime
2013-05-21 09:10:30  <- datetime 

다른 팁

According to php.net, this format may actually be invalid; there's a list of acceptable formats here: http://www.php.net/manual/en/datetime.formats.php - day of the week is not mentioned.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top