Home  >  Article  >  Backend Development  >  How to convert time to timestamp in php

How to convert time to timestamp in php

藏色散人
藏色散人Original
2020-11-09 13:58:362277browse

In PHP, you can use the strtotime function to convert time into a timestamp. The syntax is "strtotime(time,now);". The parameter time represents the date and time string, and the parameter now is used to calculate the return value. timestamp.

How to convert time to timestamp in php

Recommended: "PHP Video Tutorial"

strtotime() function describes the date or time of any English text Resolves as a Unix timestamp (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);

Parameters

time is 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
echo(strtotime("now") . "<br>");
echo(strtotime("5 September 2016") . "<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"));
?>

The running result is:

1604901378
1473004800
1604919378
1605506178
1605790583
1605456000
1604764800

The above is the detailed content of How to convert time to timestamp in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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