Mktime function to generate time stamp of any date and time
We can generate number of seconds measured since the Unix Epoch (January 1 1970 00:00:00 GMT) for any given date and time by using mktime function of PHP. Here is the general syntax of mktime() function of PHP.
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.
Let us try to find out the time elapsed on 1st January 2007, 22 hours and 30 minutes & 0 seconds. We will write this code.
The output of the above code is 1167670800. Using above output let us use date function find out the date.
By using time() function we can get the seconds elapsed since Unix Epoch (January 1 1970 00:00:00 GMT) and current time.
By using mktime() we get the seconds elapsed since Unix Epoch (January 1 1970 00:00:00 GMT) and given date and time.
When we give current date and time as input to mktime() we get the same output as time() function.
echo "Output of time() : ".time();
echo "output of mktime(): ".mktime(date('H,i,s,m,d,Y',time()));
Output is here ( Refresh this page to see how the output changes )
Output of time() : 1695808781 output of mktime():1695808781
Here we used date function to generate required format as input for mktime() function.
How many seconds elapsed since starting of this year ?
$year=date('Y');// Current Year
//time stamp of starting of the year
echo "Time elapsed in seconds:".$elapsed_time;