PHP | mktime() Function

PHP mktime() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mktime() Function.

The built-in PHP mktime() function accepts hours, minutes, seconds, month, day, year as parameters (which represents a date) and returns the Unix timestamp for the given date. If you have not yet passed any parameters to this function, then it returns the current timestamp.

Syntax

Following below is the syntax to use this function -

mktime($hour, $minute, $second, $month, $day,$ year, $is_dst)


Parameter Details

Sr.NoParameter & Description
1

hours(Mandatory)

This is an integer value representing the number of hours of the day, from its start.

2

minute(Mandatory)

This is an integer value representing the number of minutes of an hours, from its start.

3

seconds(Optional)

This is an integer value representing the number seconds of a minute, from its start.

4

month(Mandatory)

This is an integer value representing the month of an year, which should be between 1 and 12.

5

day(Mandatory)

This is an integer value representing the day of a date, it should be below the allowed number of days in the given month.

6

year(Mandatory)

This is an integer value representing the year of a date, it should be between 1 and 32767.

7

is_dst(Mandatory)

This parameter can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default)


Return Value

This function returns the Unix timestamp which represents the given date. In case of failure it returns the boolean value false.

PHP Version

This function was first introduced as part of the core PHP v 4.0 and, it works with all the later versions.

Example1

Following example illustrates the usage of the PHP mktime() function -

<?php
   $timestamp = mktime();   
   print($timestamp);
?>

Output

When the above code is executed, it will produce the following result -

1606069880

Example2

Let us now invoke this function passing the needed parameters -

<?php
   $timestamp = mktime(7, 36, 45, 06, 25, 2017);   
   print($timestamp);
?>

Output

When the above code is executed, it will produce the following result -

1498376205

Example3

Try the following example -

<?php
   $lastday = mktime(0, 0, 0, 3, 0, 2010);
   echo strftime("Last day in Feb 2010 is: %dn", $lastday);   
   $lastday = mktime(0, 0, 0, 4, -31, 2010);
   echo strftime("Last day in Feb 2010 is: %d", $lastday);
?>

Output

When the above code is executed, it will produce the following result -

Last day in Feb 2010 is: 28nLast day in Feb 2010 is: 28


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP strftime() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.