Home  >  Article  >  Backend Development  >  What is the function to convert time to timestamp in php

What is the function to convert time to timestamp in php

青灯夜游
青灯夜游Original
2021-07-08 12:11:461855browse

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)".

What is the function to convert time to timestamp in php

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);
##Optional. Specified year. 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).
Parameters Description
hour Optional. Specified hours.
minute Optional. prescribed points.
second Optional. Specifies seconds.
month Optional. Specified month.
day Optional. Specify days.
year
is_dst Note: This parameter is deprecated in PHP 5.1.0. Instead, new time zone handling features are used.
Return value: Returns an integer Unix timestamp, or FALSE if error occurs.

Example:


<?php
echo mktime(20, 20, 20, 07, 08, 2021);
?>

Output:


1625055620

Parameters 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() ] )

ParametersDescriptionRequired. Specifies a date/time string. Optional. Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.
time
now
#Return value: Returns the timestamp on success, and FALSE on failure.

Example:


<?php
echo strtotime("2021-10-21 16:00:10")."<br>";

echo strtotime("10 September 2021")."<br>";

echo strtotime("+1 day");//输出明天此时的时间戳
?>

Output:

1634803210
1631203200
1625803632

Recommended 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!

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