PHP mktime function – custom timestamps

PHP mktime function – custom timestamps

PHP mktime function is a unique function for date and time calculations. Developers fall in love with this function because this function helps create time as and when needed. Yes! It does create time in a way. So whatever time or date you supply is converted into a time value. Isn’t that great!? Well, once you get to know the whole of it you would love the concept even more.

PHP mktime function has a basic concept to return a timestamp whenever the parameters are set. As parameters, the function accepts date details and time details. The hour, minute, second, month, day, year are all sequentially supplied as the parameter list and the function returns the UNIX timestamp value for it. So, the return value is ‘long int’ and is the count of the number of seconds between Jan 1 1970 00:00:00 and the date-time supplied.

PHP mktime function – the benefits

In one of my articles, PHP-MySQL date format, I had mentioned how useful it is to use the date function for displaying custom date and time in a custom format date object. You can even compose date for insertion in database.

PHP mktime function on the other hand does something completely different. The function creates timestamps of dates that you might find difficult to figure out. Let us say for example, we need to find the weekly day for the year 2020’s 5th October. Any idea how you could figure that out? PHP mktime can do that for you with a little help from PHP date function. Take a look –

echo “Oct 5, 2020 is a ”date(“l”, mktime(0, 0, 0, 10, 5, 2020));  
// This will print Oct 5, 2020 is a Monday
// ‘l’ will give the full day name e.g. Monday and mktime will
//      provide the timestamp for that required date and time.

That’s the magic of the PHP mktime function. Whenever you need a timestamp for a particular date-time that is beyond calculation, you can use mktime function to do that for you.

PHP mktime function – some tricks and tips

There are some nooks and corners of this function that you must know as a developer. Whenever you make use of this function, be very sure about these points I am mentioning here. It would come of good use to you.

First, PHP mktime function has the auto correct feature. So, whenever the function has wrong inputs it takes up corrected values automatically. Here are some examples –

echo date(“M-d-Y”, mktime(0, 0, 0, 12, 32, 2013));  
// the above will automatically print Jan-01-2014
echo date(“M-d-Y”, mktime(0, 0, 0, 13, 1, 2013));  
// the above code will also print Jan-01-2014

Second, PHP mktime function gives you last date of a month in timestamp format. This helps in calculating leap years. Just supply the day parameter as 0 and that will signify as the last day of the previous month. Often developers also write the 0 value in negative and increase the month value by one. All the same and gives the same value. Here’s the code example –

$day = mktime(0, 0, 0, 3, 0, 2012);
echo strftime(“Last day is : %d”, $day); // prints as Last day is : 29

That’s all about mktime function. Try out your own attempts and let me know about your results in the comments section.

Comments are closed.