Home > Article > Backend Development > What is the function to convert time to timestamp in php
Function to convert time to timestamp: 1. mktime(), which can obtain the timestamp from the date and return a Unix timestamp. The syntax is "mktime(hour, minute, second, month, day, year);" ; 2. strtotime(), which can parse the time description of any English text into a timestamp, with the syntax "strtotime (time)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
1. mktime() function
mktime() function is used to get the timestamp from the date, and returns the timestamp successfully, otherwise it returns FALSE.
Syntax:
mktime(hour,minute,second,month,day,year,is_dst);
Parameters | Description |
---|---|
hour | Optional. Specified hours. |
minute | Optional. prescribed points. |
second | Optional. Specifies seconds. |
month | Optional. Specified month. |
day | Optional. Specify days. |
year | ##Optional. Specified year.|
is_dst | Optional. Set to 1 if the time is during daylight saving time, 0 otherwise, or -1 (default) if unknown. If unknown, PHP will try to find it itself (possibly producing unexpected results).Note: This parameter is deprecated in PHP 5.1.0. Instead, new time zone handling features are used. |
<?php echo mktime(20, 20, 20, 07, 08, 2021); ?>Output:
1625055620Parameters can be omitted from right to left, any omitted parameters will be set to the local date and time current value. mktime() is useful for doing date calculations and validation, it will automatically calculate the correct value for out-of-range input. For example, the following example outputs 2008-01-01:
<?php echo date("Y-m-d", mktime(0, 0, 0, 12, 32, 2007)); echo date("Y-m-d", mktime(0, 0, 0, 13, 1, 2007)); ?>
2, strtotime()
strtotime() function is used to represent English text strings Convert date to timestamp, which is the inverse function of date(). It returns timestamp successfully, otherwise it returns FALSE. Syntax:strtotime ( time [, now = time() ] )
Description | |
---|---|
time | 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.
<?php echo strtotime("2021-10-21 16:00:10")."<br>"; echo strtotime("10 September 2021")."<br>"; echo strtotime("+1 day");//输出明天此时的时间戳 ?>Output:
1634803210 1631203200 1625803632Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of What is the function to convert time to timestamp in php. For more information, please follow other related articles on the PHP Chinese website!