Differences between time of and mktime of methods in php
- 2020-09-16 07:24:31
- OfStack
The time() function returns the current time. The main function of the mktime() function is not to return the current time, but to format the time. Although writing mktime() alone without any parameters such as echo mktime() and echo time() has the same effect. But it's not the same thing.
PHP mktime () function
PHP Date/Time functions
Definition and usage
The mktime() function returns an Unix timestamp of 1 date.
The parameter always indicates the GMT date, so is_dst has no effect on the result.
The parameters can be left to right and are set to the current GMT value.
grammar
mktime(hour,minute,second,month,day,year,is_dst)
Parameters to describe
hour optional. Set an hour.
minute optional. Set aside a minute.
second optional. Seconds.
month optional. Specify numerically represented months.
day optional. Day.
year optional. Provisions of years. On some systems, the legal value is between 1901 and 2038. This limitation no longer exists in PHP 5.
is_dst
Optional. Set to 1 if the time is during daylight Saving time (DST), 0 if not, -1 if not known.
As of 5.1.0, the is_dst parameter has been discarded. You should therefore use the new time zone processing feature.
Hints and comments
Note: Before PHP 5.1, if the argument to this function was illegal, false was returned.
example
The mktime() function is useful for date arithmetic and validation. It automatically corrects out-of-bounds inputs:
<?php
echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,99)));
?>
Output:
Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999
PHP time () function
PHP Date/Time functions
Definition and usage of time()
The time() function returns an Unix timestamp for the current time.
grammar
time(void)
Parameters to describe
void optional.
instructions
Returns the number of seconds from the Unix era (00:00:00 GMT, 1 January 1970) to the current time.
Hints and comments
Tip: As of PHP 5.1, the timestamp of the time the request originated is saved in $_SERVER['REQUEST_TIME'].
example
Example 1
<?php
$t=time();
echo($t . "<br />");
echo(date("D F d Y",$t));
?>
Output:
1138618081
Mon January 30 2006
Example 2
<?php
$nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
?>
Output:
Now: 2005-03-30
Next Week: 2005-04-07