PHP mktime

 The PHP mktime function is used to obtain the Unix timestamp. You can set the hour, minute, second, month, day, and year to be formatted by yourself, and let mktime help you convert it to Unix timestamp, which is represented by Unix timestamp It means that from 00:00:00 GMT on January 1, 1970, to the set time difference in seconds, it is very useful when the system needs time calculation, because it is presented in seconds, so it can be added and subtracted directly. It is also easy to convert the number of seconds back to other time formats.


Basic syntax of PHP mktime function
int mktime( int $hour, int $minute, int $second, int $month, int $day, int $year, int $is_dst );
The above parameters are brought into mktime to return Unix timestamp. The parameter order here is not the same as the parameter order in Unix mktime( ). The parameters in the PHP mktime function can be omitted from right to left, and cannot be omitted. When any parameter is omitted, PHP will automatically calculate it based on the current server time. If all parameters are omitted, it will return the current Unix timestamp.

PHP mktime function parameter list
int $hour
Time, you can directly give a number or use date("H") to let PHP read the time of the server at that time.
int $minute
Minutes, you can directly give numbers or use date("i") to let PHP read the current server minutes.
int $second
For seconds, you can directly give numbers or use date("s") to let PHP read the seconds of the server at that time.
int $month
Month, you can directly give a number or use date("n") to let PHP read the month of the server at that time.
int $day
For the day, you can directly give a number or use date("j") to let PHP read the day of the server at that time.
int $year
Year, you can directly give a number or use date("Y") to let PHP read the year of the server at that time.
int $is_dst
It can be omitted, this parameter has been discarded in versions after PHP 5.1.0.

When the program is not used to calculate a fixed time difference, but to calculate the time difference at the time, it is very convenient to use the PHP date function .

PHP mktime example
<?php
echo mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y")).' <br>';
echo mktime();
?>
The output results of the above two examples are the same. The first line uses the PHP date function to fill "hour, minute, second, month, day, and year" into the mktime function in order. The second line is to fill all Omit all the parameters, let PHP capture "hour, minute, second, month, day, year" according to the server time, the result is the same.

PHP mktime automatic adjustment
<?php
echo date("MdY", mktime(0, 0, 0, 13, 33, 1988)).'<br>'; // Automatically adjust to Feb-02-1989
echo date("MdY", mktime(0, 0, 0, 8, 1, 2013)).'<br>'; // Automatically adjust to Aug-01-2013
echo date("MdY", mktime(0, 0, 0, 0, 1, 2013)).'<br>'; // Automatically adjust to Dec-01-2012
echo date("MdY", mktime(0, 0, 0, 1, 1, 01)).'<br> '; // Automatically adjust Jan-01-2001
?>
I have to admire the automatic adjustment function of PHP mktime. It can automatically adjust the wrong or exceeded time value to the correct value. For example, in the first example, if the year and month are both out of the normal range, mktime is automatically added up and the original The third example is also very good. We deliberately write the month as zero, and mktime will automatically reduce the original date from October 1, 2013 to December 2012. The 1st day is equal to one month automatically, and it becomes the correct time, very easy to use.

Post a Comment

0 Comments