Categories
alexwebdevelop.com

PHP tip 3: Date operations with mktime

The mktime() function returns the Unix Timestamp of a specific date-time, but it has another incredibly useful feature: it can perform date-time operations.

[et_pb_section fb_built=”1″ _builder_version=”3.0.93″][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_image src=”https://alexwebdevelop.com/wp-content/uploads/2018/06/date-operations-mktime.jpg” alt=”Date operations with mktime()” title_text=”Date operations with mktime()” align=”center” _builder_version=”3.7″ custom_margin=”||5px|” custom_padding=”|||”][/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″]

DATE OPERATIONS WITH MKTIME()

[/et_pb_text][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

mktime() is a very versatile function.

Its main purpose is to get the Unix Timestamp of a specific date-time (to learn more about time and PHP, you can read my PHP Time Handling guide: part 1, part 2 and part 3).

 

But mktime() has another, less well known and incredibly useful feature: it can perform date-time operations.

Each of the function’s arguments represents a date-time component: hour, minute, second, month, day and year.

 

mktime() accepts any integer number for each component, including values outside of the valid range of that component. Even negative integers are accepted.

 

 

[/et_pb_text][et_pb_code _builder_version=”3.7″][easy-tweet tweet=”Alex’s PHP tip: Date-time operations with mktime()” via=”no” hashtags=”PHP”][/et_pb_code][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

When a value is outside of the valid range, the function calculates the difference from the nearest valid value and then adds, or subtracts, an equal number of component units to the date-time before returning the Unix Timestamp.

 

It’s really easier to see how it works with a few examples:

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ text_font=”||||||||” text_font_size=”13px”]

<?php

/* Get the Unix Time of the beginning of the next year */
mktime(0, 0, 0, 1, 1, date('Y') + 1);

/* Yesterday at midnight;
   without worrying about day and month number or daylight saving time */
mktime(0, 0, 0, date('n'), date('j') - 1);

/* 2 months from now, same day and time;
   without worrying about the number of days or daylight saving time */
mktime(date('H'), date('i'), date('s'), date('n') + 2);

/* Midnight, last day of the last month;
   simply as the "day 0" of the current month */
mktime(0, 0, 0, date('n'), 0);

/* 72 hours before now;
   without worrying about day/month/year changes or daylight saving time */
mktime(date('H') - 72);

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

Adding or subtracting date components can be really useful.

 

Suppose that you need to get the Unix Timestamp of “last day of the previous monthâ€.

If you had to provide mktime() with the exact date-time components, you would need to get the current month number and subtract 1 from it, then check if it is 0 (meaning that we are in January, so you would have to set it to 12 and decrease the year by 1), then get the current day number and check how many days the previous month has…

It’s long and boring.

 

But thanks to mktime(), you can just set 0 as the day value to achieve the same result:

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.5.1″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.7″ text_font=”||||||||” text_font_size=”13px”]


$time = mktime(0, 0, 0, date('n'), 0, date('Y'));

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”|||”]

 

 

 

If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

[/et_pb_text][et_pb_text _builder_version=”3.2.2″]

 

Alex

[/et_pb_text][et_pb_code _builder_version=”3.10.1″][/et_pb_code][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Reply

Your email address will not be published. Required fields are marked *