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

How to convert date to timestamp in php

藏色散人
Release: 2023-03-09 15:30:02
Original
9196 people have browsed it

php method to convert date to timestamp: first create a PHP sample file; then directly use the strtotime function syntax statement such as "strtotime('2010-03-24 08:15:42');" Just convert the date into a timestamp.

How to convert date to timestamp in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Function used to convert date to UNIX timestamp: strtotime()

General form: strtotime('2010-03-24 08:15:42');

strtotime() function converts any English text date or time descriptions are parsed into Unix timestamps (number of seconds since January 1 1970 00:00:00 GMT).

Note: If the year representation uses a two-digit format, values ​​0-69 will be mapped to 2000-2069, and values ​​70-100 will be mapped to 1970-2000.

Note: Please note that for dates 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 delimiter is a dash (-) or a dot (.), the European d-m-y format is used. To avoid potential errors, you should use YYYY-MM-DD format whenever possible or use the date_create_from_format() function.

Syntax

strtotime(time,now);
Copy after login

Example

Parse English text date and time into Unix timestamp:

<?php
echo(strtotime("now") . "<br>");
echo(strtotime("15 October 1980") . "<br>");
echo(strtotime("+5 hours") . "<br>");
echo(strtotime("+1 week") . "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
echo(strtotime("next Monday") . "<br>");
echo(strtotime("last Sunday"));
?>
Copy after login

Output:

1620696264
1473004800
1620714264
1621301064
1621585469
1621180800
1620489600
Copy after login

Recommended learning: "PHP video tutorial"

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!

Related labels:
php
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