PHP strtotime() function is used to accept string parameters representing date and time, and parse the date or time description of any English text into a Unix timestamp. The syntax is "strtotime (time,now)", if successful Returns the timestamp, otherwise returns FALSE.
How to use the PHP strtotime() function?
The strtotime() function is a built-in function in PHP.
The strtotime() function accepts a string parameter representing a date and time, and parses any date or time description in English text into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT); For example, "now" refers to the current date. This function returns the number of seconds since the Unix Epoch. We can also use date() function to return English text date time in date format.
Basic syntax:
strtotime(time,now);
Parameters: strtotime() function accepts two parameters
● Time: Specify an English text time or date description indicating the date or time to be returned. This function parses a string and returns the time in seconds; cannot be omitted.
● now: Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.
NOTE: Since the time/date is not static, the output will vary.
Let’s take a look at the use of PHP strtotime() function through code examples
Example 1: Display the current time
<?php header("content-type:text/html;charset=utf-8"); //以秒为单位显示当前时间 echo "以秒为单位显示当前时间:".strtotime("now"), "<br>"; // 以日期格式显示当前时间 echo "以日期格式显示当前时间:".date("Y-m-d", strtotime("now"))."\n"; ?>
Output:
以秒为单位显示当前时间:1556162013 以日期格式显示当前时间:2019-04-25
Example 2: Display the time and date described by "12th february 2017"
<?php header("content-type:text/html;charset=utf-8"); // 以秒为单位显示转换后的日期时间 echo "以秒为单位显示:".strtotime("12th february 2017")."<br>"; // 以日期格式显示转换后的日期时间 echo "以日期格式显示:".date("Y-m-d", strtotime("12th february 2017"))."<br>"; ?>
Output:
以秒为单位显示:1486854000 以日期格式显示:2017-02-12
Example 3: Display The date of next Sunday at the current time
<?php header("content-type:text/html;charset=utf-8"); // 以秒为单位显示转换后的日期时间 echo "以秒为单位显示:".strtotime("next sunday")."<br>"; // 以日期格式显示转换后的日期时间 echo "以日期格式显示:".date("Y-m-d", strtotime("next sunday"))."<br>"; ?>
Output:
以秒为单位显示:1556402400 以日期格式显示:2019-04-28
Related video tutorial recommendation: "PHP Tutorial"
The above is this article The entire content, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to use php strtotime function. For more information, please follow other related articles on the PHP Chinese website!