Home > Backend Development > PHP Problem > How to convert date to timestamp (timestamp) in php

How to convert date to timestamp (timestamp) in php

青灯夜游
Release: 2023-03-10 10:36:02
Original
3348 people have browsed it

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)".

How to convert date to timestamp (timestamp) in php

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>";    //输出明天此时的时间戳 
?>
Copy after login

Output:

1622707210
1631203200
1622787020
Copy after login

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] )
Copy after login
ParameterDescription
timeRequired. Specifies a date/time string.
nowOptional. 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>";
?>
Copy after login

Output:

1622700860
1622700860
1631203200
1622787260
1623305660
1623492862
1623254400
1622390400
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template