The mktime() function creates Unix timestamp from year, month, day, hour, second in separate variables. It has the following format.
PHP mktime() Function has the following syntax.
int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
The parameter are: hour, minute, second, month, day, year, is_dst.
Parameter | Is Required | Description |
---|---|---|
hour | Optional. | hour value |
minute | Optional. | minute value |
second | Optional. | second value |
month | Optional. | month value |
day | Optional. | day value |
year | Optional. | year value |
PHP mktime() Function returns an integer Unix timestamp. FALSE on error.
Note that the hour should be in 24-hour clock time. So, to pass in 10:30 p.m. on the 21th of June 2012, you would use mktime() like this:
<?PHP
$unixtime = mktime(22, 30, 0, 6, 21, 2012, -1);
print_r($unixtime);
?>
The code above generates the following result.
The following code shows how to calculate total hours.
<?php//from w ww . ja v a2s. c om
$now = mktime();
$taxday = mktime(0,0,0,4,15,2010);
// Difference in seconds
$difference = $taxday - $now;
// Calculate total hours
$hours = round($difference / 60 / 60);
echo "Only $hours hours until tax day!";
?>
The code above generates the following result.
The following code shows how to return the Unix timestamp for a date. Then use it to find the day of that date.
<?php
echo "Oct 3, 2014 was on a ".date("l", mktime(0,0,0,10,3,2014));
?>
The code above generates the following result.