In PHP, you can use the strtotime() function to convert date (date) to timestamp (timestamp). This function can parse the date and time description of any string into a Unix timestamp. The syntax format is " strtotime(date as string)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will convert date (date) For timestamp (timestamp)
<?php echo strtotime("2021-06-03 16:00:10")."<br>"; //输出 1620979210 echo strtotime("10 September 2021")."<br>"; //输出 1631203200 echo strtotime("+1 day"), "<br />"."<br>"; //输出明天此时的时间戳 ?>
Output:
1622707210 1631203200 1622787020
Description:
strtotime() function will Any string datetime description resolves to a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT). This function is the inverse function of date() and returns a timestamp if successful, otherwise it returns FALSE. Syntax:
int strtotime ( string time [, int now] )
Parameter | 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. |
Example:
<?php // 设置时区 date_default_timezone_set("PRC"); echo strtotime("now")."<br>"; echo strtotime("now")."<br>"; echo strtotime("10 September 2021")."<br>"; echo strtotime("+1 day")."<br>"; echo strtotime("+1 week")."<br>"; echo strtotime("+1 week 2 days 4 hours 2 seconds")."<br>"; echo strtotime("next Thursday")."<br>"; echo strtotime("last Monday")."<br>"; ?>
Output:
1622700860 1622700860 1631203200 1622787260 1623305660 1623492862 1623254400 1622390400
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert date to timestamp (timestamp) in php. For more information, please follow other related articles on the PHP Chinese website!