Home  >  Article  >  Backend Development  >  Detailed explanation of php strtotime() function to parse date and time into UNIX timestamp example

Detailed explanation of php strtotime() function to parse date and time into UNIX timestamp example

怪我咯
怪我咯Original
2017-04-18 10:43:083503browse

In PHP we use the strotime() function to parse the date and time of any English text into a UNIX timestamp. Its syntax is as follows:

strotime() function syntax format

strtotime(time,now);

The value is relative to the time given by the parameter now. If this parameter now is not provided, use The current system time.

Parameter details:

This function has two parameters

time

The parsed string, format Syntax according to GNU » date input format. Before PHP 5.0, milliseconds were not allowed in time. Since PHP 5.0, they are allowed but will be ignored.

now

The timestamp used to calculate the return value. The default value of this parameter is the current time (time()), which can also be set to a timestamp () of other times.

Return value: Returns the timestamp if successful, otherwise returns FALSE. Before PHP 5.1.0, this function returned -1 on failure, and later versions returned false.

The first parameter of strtotime can be our common English time format, such as "2008-8-20" or "10 September 2000" and so on. It can also be a time description based on the parameter now, such as "+1 day" and so on.

strotime() function example

Example one

Use the strotime() function to obtain the unix timestamp of the specified date, the code is as follows

<?php
echo strtotime("2017-4-10");

?>

Run result:

Detailed explanation of php strtotime() function to parse date and time into UNIX timestamp example

Example description: Return the timestamp of 0:00:00 on April 10, 2017


Example 2

This example uses the strotime() function to obtain the UNIX timestamp of the date and time string in English format, and outputs part of the time. The example code is as follows

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

echo strtotime("now")." ";                                              //当前时间的时间戳
echo "输出时间:".date("Y-m-d H:i:s",strtotime("now"))."<br/>";        //输出当前时间


echo strtotime("10 May 2017")." ";                                     //输出指定日期的时间戳
echo "输出时间:".date("Y-m-d H:i:s",strtotime("10 May 2017"))."<br/>";        //输出指定日期的时间


echo strtotime("+3 day")." ";                                           //输出当前时间三天后的时间戳
echo "输入三天后的时间:".date("Y-m-d H:i:s",strtotime("+3 day"))."<br/>";

echo strtotime("+1 week")." ";
echo "输出下个星期此时的时间:".date("Y-m-d H:i:s",strtotime("+1 week"))."<br/>";

echo strtotime("+1 week 2 days 3 hours 4 seconds")."<br/>";

echo strtotime("NEXT Thursday")."<br/>";

echo strtotime("last Monday");

?>

Code running results :

Detailed explanation of php strtotime() function to parse date and time into UNIX timestamp example

The above is the simple function completed by our strotime() function. In the next section, we will use the several time functions we learned earlier, including our mktime() function to obtain the local timestamp , and the time() function to obtain the current timestamp , ## The #date() function gets the current date and time, the getdate() function gets the date information and the checkdate() function checks the validity of the date, etc., through these functions, Complete our application with date and time.


The above is the detailed content of Detailed explanation of php strtotime() function to parse date and time into UNIX timestamp example. 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