The strtotime() function in PHP parses the date and time description of any English text into a Unix timestamp. This function is also something we often use. Friends in need can refer to it.
Grammar
strtotime(time,now) parameter description
time specifies the time string to be parsed.
now is the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.
Example
The code is as follows | Copy code | ||||||||
echo(strtotime("+1 week"));
?>Output: 1138614504 |
The code is as follows | Copy code |
echo strtotime("now"), "n"; echo strtotime("10 September 2000"), "n"; echo strtotime("+1 day"), "n"; echo strtotime("+1 week"), "n"; echo strtotime("+1 week 2 days 4 hours 2 seconds"), "n"; echo strtotime("next Thursday"), "n"; echo strtotime("last Monday"), "n"; ?> |
The code is as follows | Copy code |
$str = 'Not Good'; // previous to PHP 5.1.0 you would compare with -1, instead of false if (($timestamp = strtotime($str)) === false) { echo "The string ($str) is bogus"; } else { echo "$str == " . date('l dS of F Y h:i:s A', $timestamp); } ?> I just found out PHP thinks slashes in date-formats aren't very european: (I guess this is not a bug, just the way it works. But correct me if I'm wrong.) $date = "06/10/2011 14:28"; // 6 October 2011 2:28 pm $otherDate = "06-10-2011 14:28"; // 6 October 2011 2:28 pm Echo $stamp = strtotime($date) . " "; // outputs 1307708880 echo $otherStamp = strtotime($otherDate) . " "; // outputs 1317904080 echo date("d-m", $stamp); // outputs 10-06 echo date("d-m", $otherStamp); // outputs 06-10 ?> |