PHP Tutorial - PHP mktime() Function






Definition

The mktime() function creates Unix timestamp from year, month, day, hour, second in separate variables. It has the following format.

Syntax

PHP mktime() Function has the following syntax.

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

Parameter

The parameter are: hour, minute, second, month, day, year, is_dst.

ParameterIs RequiredDescription
hourOptional.hour value
minuteOptional.minute value
secondOptional.second value
monthOptional.month value
dayOptional.day value
yearOptional.year value




Return

PHP mktime() Function returns an integer Unix timestamp. FALSE on error.

Example

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.





Example 2

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.

Example 3

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.