PHP strtotime()Function
PHP Date/Time Reference Manual
Examples
Parse a date/time description of any string into a Unix timestamp:
//Set timezone
date_default_timezone_set("PRC");
$time = strtotime("2018-01-18 08:08:08"); //Convert a specified date to a timestamp
//Print current time PHP_EOL newline, compatible with different systems
echo $time, PHP_EOL;
//More Examples
echo strtotime("now"), PHP_EOL;
echo strtotime("now"), PHP_EOL;
echo strtotime("10 September 2000"), PHP_EOL;
echo strtotime("+1 day"), PHP_EOL;
echo strtotime("+1 week"), PHP_EOL;
echo strtotime("+1 week 2 days 4 hours 2 seconds"), PHP_EOL;
echo strtotime("next Thursday"), PHP_EOL;
echo strtotime("last Monday"), PHP_EOL;
Output result:
1516234088 1517408272 1517408272 968515200 1517494672 1518013072 1518200274 1517414400 1517155200
Definition and Usage
The strtotime() function parses a date/time description of any string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Note:If the year is expressed in a two-digit format, the values 0-69 are mapped to 2000-2069 and the values 70-100 are mapped to 1970-2000.
Note:Please note the date in m/d/y or d-m-y format; if the separator is a slash (/), the American m/d/y format is used. If the separator is a dash (-) or a dot (.), the European d-m-y format is used. To avoid potential errors, you should use the YYYY-MM-DD format whenever possible, or use the date_create_from_format() function.
Syntax
int strtotime ( string $time [, int $now = time() ] )
| Parameters | Description |
|---|---|
| time | Required. Specifies a date/time string. |
| now | Optional. Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used. |
Technical Details
| Return value: | Returns a timestamp on success, FALSE on failure. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | PHP 5.3.0: Relative time formats, such as this week, previous week, last week, next week, now specify a week from Monday to Sunday, rather than using 7 days before/after the current date/time. PHP 5.3.0: 24:00 is now a valid format. PHP 5.2.7: In earlier versions, if a given day in a month was requested and that day happened to be the first day of the month, one week would be incorrectly added to the returned timestamp. This has now been corrected. PHP 5.1.0: Returns FALSE on failure (previously -1), and E_STRICT and E_NOTICE timezone errors were added. PHP 5.0.2: 'now' and other relative times are now correctly calculated based on the current time, rather than the time at midnight today. PHP 5.0.0: Microseconds are allowed (but microseconds are usually ignored). |
PHP Date/Time Reference Manual Other Extensions