Home >Backend Development >PHP Problem >How to convert date to timestamp in php
How to convert date into timestamp in php: You can use strtotime() function to achieve this. The strtotime() function can parse the date and time description of any string into a Unix timestamp, returning the timestamp if successful, and false on failure.
The strtotime() function parses any string datetime description into a Unix timestamp, returning a timestamp on success or FALSE on failure.
(Recommended tutorial: php graphic tutorial)
Syntax:
int strtotime ( string $time [, int $now = time() ] )
Parameters:
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.
(Video tutorial recommendation: php video tutorial)
Example:
<?php // 设置时区 date_default_timezone_set("PRC"); $time = strtotime("2018-01-18 08:08:08"); // 将指定日期转成时间戳 // 打印当前时间 PHP_EOL 换行符,兼容不同系统 echo $time, PHP_EOL; // 更多实例 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; ?>
The above is the detailed content of How to convert date to timestamp in php. For more information, please follow other related articles on the PHP Chinese website!