The PHP strtotime() function claims to be able to handle "noon" and "midnight" but it's not working for me.
$noon_timestamp = strtotime("August 3 noon");
print date("Y-m-d H:i:s", $noon_timestamp);
1969-12-31 19:00:00
However, this does work:
$twelve_timestamp = strtotime("August 3 12:00 PM");
print date("Y-m-d H:i:s", $twelve_timestamp);
2015-08-03 12:00:00
I found this did work:
$noon_timestamp = strtotime("noon August 3");
print date("Y-m-d H:i:s", $noon_timestamp).PHP_EOL;
2015-08-03 12:00:00
But I'm not sure why I need to move "noon" to before the date. Seems like it should work either way. I know users will try both methods.
EDIT: I think I've found a relevant note in the PHP docs under Relative Formats:
Exceptions to this rule are: "yesterday", "midnight", "today", "noon" and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are different. Considering today's date of "July 23rd, 2008" the first one produces "2008-07-24 11:00" where as the second one produces "2008-07-24 00:00". The reason for this is that those five statements directly influence the current time.
I don't understand the last sentence. "Directly influence the current time" ? That seems irrelevant to the order. "tomorrow 11:00" and "11:00 tomorrow" mean the same thing in English.
I'm not sure this is a general solution, but I made this function:
function arrange_timestring($timestring) {
$problematic_keywords = array("noon","midnight");
$keyword_counter = 0;
foreach($problematic_keywords as $keyword) {
if (strpos($timestring,$keyword) !== false) {
$keyword_counter += 1;
if (strpos($timestring,$keyword) > 0) {
$timestring = str_replace($keyword, "", $timestring);
$timestring = $keyword." ".$timestring;
}
}
}
if ($keyword_counter > 1) {
return Null; //strings shouldn't have both noon and midnight
} else {
return $timestring;
}
}
Then my code works as expected:
$noon_timestamp = strtotime(arrange_timestring("August 3 noon"));
$twelve_timestamp = strtotime(arrange_timestring("August 3 12:00 PM"));
print date("Y-m-d H:i:s", $noon_timestamp);
2015-08-03 12:00:00
print date("Y-m-d H:i:s", $twelve_timestamp);
2015-08-03 12:00:00
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments