mktime (hour, minute, second, month, day, year)
This function is useful in doing date calculations like finding difference in two dates. We will learn how to use mktime() function along with date function to generate and check different events.$d1=mktime(22,30,0,1,1,2007);
echo $d1;
The output of the above code is 1167670800.$d1=mktime(22,30,0,1,1,2007);
echo $d1;
echo "<br>";
echo date("m/d/Y H:i:s",$d1);
As expected the out put of last line is 01/01/2007 22:30:00
$d1=mktime(22,30,0,1,0,2007);
The output of this function is 12/31/2006 22:30:00 . <?Php
echo "Output of time() : ".time();
echo "<br>";
//echo "output of mktime(): ".mktime(date('H,i,s,m,d,Y',time()));
echo "output of mktime(): ".mktime((int)date('H,i,s,m,d,Y',time()));
?>
Output is here ( Refresh this page to see how the output changes )
Output of time() : 1760722768
output of mktime():1760722768
Here we used date function to generate required format as input for mktime() function.
$year=date('Y');// Current Year
//time stamp of starting of the year
$time1=mktime(0,0,0,01,01,$year);
$elapsed_time=time()-$time1;
echo "Time elapsed in seconds:".$elapsed_time;
Refresh the page to check change in output Time elapsed in seconds:25033168
Function | Description |
---|---|
strtotime() | Text string of data to timestamp |
gettimestamp() | Getting Timestamp from date object |
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.