Understanding Time, Mktime, and Strtotime Function in PHP

Today, we’ll discuss time, mktime, and strtotime function in PHP.

Both mktime and strtotime functions have the same functionalities, they generate time in timestamp format. The difference is for strtotime() function, the accepted argument is string(text), while for the mktime() function, the accepted argument is an integer in a unit of time.

Timestamp

The functions that we discuss here are all associated with a timestamp, therefore, we need to know what the timestamp is.

Timestamp is a term that refers to the standard time in seconds since the epoch time ( 1970-01-01 00:00:00 ), thus, 1 in the timestamp means 1 second since 1970-01-01 00:00:00 that is 1970 01-01 00:00:01, while 60 means 1970-01-01 00:01:00, etc..

I. time() Function In PHP

The time() function is used to retrieve the current time on the server computer in a timestamp format. This function has no arguments.

Timestamp that generated by the time() function is always in UTC time zone (GMT + 0), even if the timezone configuration not in GMT + 0. More about timezone, can be read in the article: Understanding Time Zone (Timezone) and Time Difference In PHP

Example:

<?php
echo 'Date: ' . date('Y-m-d H:i:s'); // Result: 2017-02-19 03:21:31 (GMT + 1) - my current timezone
echo 'Timestamp: ' . time(); // Result: 1487470891 (GMT + 0)

date_default_timezone_set('UTC');
echo 'Date: ' . date('Y-m-d H:i:s'); // Result: 2017-02-19 02:21:31 (GMT + 0)
echo 'Timestamp: ' . time(); // Result: 1487470891 (GMT + 0)

Result:

Date: 2017-02-19 03:21:31
Timestamp: 1487470891

Date: 2017-02-19 02:21:31
Timestamp: 1487470891

The above example shows that the result of the date() function changed after we change the timezone to UTC (GMT + 0), but the timestamp generated by the time() function is always the same: 1487470891 (GMT + 0).

The time() function is useful to get a future time or past time from the current time, for example:

echo 'Now: ' . date('Y-m-d H:i:s');
echo 'Next minute: ' . date('Y-m-d H:i:s', time() + 60);
echo 'Next hour: ' . date('Y-m-d H:i:s', time() + (60 * 60));
echo 'Next day: ' . date('Y-m-d H:i:s', time() + (60 * 60 * 24));
echo 'Next 7 days: ' . date('Y-m-d H:i:s', time() + (60 * 60 * 24 * 7))';

Result:

Now: 	     2017-02-19 03:30:04
Next minute: 2017-02-19 03:31:04
Next hour:   2017-02-19 04:30:04
Next day:    2017-02-20 03:30:04
Next 7 days: 2017-02-26 03:30:04

Although it can calculate the next and previous time, this function can only calculate the time in seconds, it can not count next week or next month. To overcome this, we can use the strtotime() function, instead.

II. mktime Function in PHP

The mktime() function, as the name implies (make time) is used to get a certain time in timestamp format.

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

Usually, we read the arguments from left to right. Well, for the mktime() function, in order to easy to understand the arguments, we can read it from left to right (for the time: H: i: s) and right to left (for the date: Y-d-m)

Argument Explanation:

  • All the arguments are optional, the default value is the current date-time:
  • mktime (date('H'), date('i'), date('s'), date('n'), date('j'), date('Y'));
  • The following functions will generate the same value of 1484701662
  • <? Php
    echo strtotime('now');
    echo mktime();
    echo time();

    The above three functions will produce the same timestamp, however, since PHP version 5.1, if we perform the mktime() function without any arguments, PHP will raise a warning that tells us to use the time() function instead.

  • All arguments are in integer form.
  • If the argument value is zero or negative, then it will produce a previous time, for example:
  • <?php
    echo date('d-m-Y', mktime(0,0,0,1,1,2017)); // 01-01-2017
    echo date('d-m-Y', mktime(0,0,0,1,0,2017)); // 31-12-2016
    echo date('d-m-Y', mktime(0,0,0,1,-1,2017)); // 30-12-2016
  • If the argument value exceeds the maximum value, then it will generate the next time, for example:
  • <?php
    echo date('d-m-Y', mktime(0,0,0,1,32,2017)); // 01-02-2017
    echo date('d-m-Y', mktime(0,0,0,1,60,2017)); // 01-03-2017
    echo date('d-m-Y', mktime(0,0,0,2,29,2017)); // 01-13-2017
    echo date('d-m-Y', mktime(0,0,0,13,1,2017)); // 01-01-2018
The mktime() function is similar to the time() function, the difference is that the mktime() function is more flexible because it accepts more arguments.

Examples of mktime() function:

<?php
echo date('d-m-Y'); // 18-01-2017
echo date('d-m-Y', mktime( 0, 0, 0, date('n'), date('j') + 30, date('y'))); // 17-02-2017
echo date('d-m-Y', mktime( 0, 0, 0, date('n') - 1, date('j'), date('y'))); // 18-12-2017

III. strtotime Function In PHP

strtotime() function in PHP, as the name implies (string to time) is used to generate a certain time in timestamp format based on a particular string. The format is:

strtotime (string, timestamp);

Explanation:

string argument can be a (1) Date or Time Format or (2)  word or sentence (in English) which refers to a specific time (Relative Format).

For Example:

echo strtotime ( 'now'); // Relative Format
echo strtotime ( '2017-02-19'); // Date format
echo strtotime ('17:10:00 '); // Time format

timestamp argument is optional, if supplied, it will be used as referenced time. Consider the following example:

<?php
$timestamp = strtotime('2017-02-28'); // 1488236400
echo date('d-m-Y', strtotime('+1 day', $timestamp)); // 01-03-2017

In the example above, the strtotime() function generates timestamp of: 1 day after February 28, 2017, that is March 01, 2017. The example above shows that the referenced date is 2017-02-28

The string argument can be grouped into two form: Date Time format and relative format.

1. Date Time format

If the form of the string argument is in a date time format, then be careful when using the separator, if it is a slash (/), then PHP will read the date in m/d/y format, e.g 01/10/2017 or 1/10/2017 which means January 10, 2017. Consider the following example:

echo strtotime ( '1/10/2017'); // Result: 1484002800
echo strtotime ('01/10/2017 '); // Result: 1484002800

If the separator is a dash (-), PHP will read the date in d-m-y format, for example: 10-1-2017 or 10-01-2017:

echo strtotime ('10-1-2017 '); // Result: 1484002800
echo strtotime ('10-01-2017 '); // Result: 1484002800

To overcome the above differences, consider to always use the standard date format of ISO 8601 that is YYYY-MM-DD, for example:

echo strtotime ('2017-01-10'); // Result: 1484002800

whereas for the time format, use the format H:i:s (hours: minutes: seconds), eg:

echo strtotime ('2017-01-10 14:12:55'); // Result: 1484053975

2. Relative Format

The Relative format is word or sentence in English that reflect a specific time, such as "now", "yesterday", etc… In addition, this format can also be combined with the date-time format, such as "yesterday 18:21"

Relative format can be symbols or day-based notation.

The symbols are as follows:

Description Format
Day name ‘sunday’ | ‘monday’ | ‘tuesday’ | ‘wednesday’ | ‘thursday’ | ‘friday’ | ‘saturday’
Day Name (short) ‘sun’ | ‘mon’ | ‘tue’ | ‘wed’ | ‘thu’ | ‘fri’ | ‘sat’
Ordinal ‘first’ | ‘second’ | ‘third’ | ‘fourth’ | ‘fifth’ | ‘sixth’ | ‘seventh’ | ‘eighth’ | ‘ninth’ | ‘tenth’ | ‘eleventh’ | ‘twelfth’
Reltext ‘last’ | ‘next’ | ‘previous’ | ‘this’
Unit* ‘year’ | ‘month’ | ‘day’ | ‘hour’ | ‘minute’ | ‘min’ | ‘second’ | ‘sec’

*) Add a suffix s for plural name, e.g. one month, plural: 2 months

For example:

echo date('Y-m-d H:i:s'); // 2017-02-19 09:37:48
echo date('Y-m-d H:i:s', strtotime('next month')); // 2017-03-19 09:37:48
echo date('Y-m-d H:i:s', strtotime('previous day')); // 2017-02-18 09:37:48
echo date('Y-m-d H:i:s', strtotime('previous hour')); // 2017-02-19 08:37:48

Day-based Notations are:

Fomat Description
‘today’ | ‘midnight’ | ‘yesterday’ | ‘tomorrow’ The time is set to 00:00:00
‘now’ Now
‘first day of’ The first day of month. Example: ‘first day of next month’ or ‘first day of may 2017’
‘last day of’ The last day of month. Example: ‘last day of previous month’
number [space] unit or week. Relative time based on a number. Example: ‘1 day’ or ‘+1 day’, ‘-1 day’, ‘+5 weeks’
‘ago’ Previous time. Example: ‘2 days ago’, ‘2 months 1 day ago’, ‘1 hour ago’
Day Name Result the next day of this name. Example: if today is Wednesday 2017-02-22 , then ‘monday’ will resulted next monday 2017-02-27
Reltext [space] Day Name [space] week Example: ‘Monday last month’ result the last monday of the current month.
Important to note that both symbols and day-based notation must be written in lowercase and are case sensitive, so ‘last day of’ and ‘Last day of’ are different

Functions related date and time are useful for many purposes such as to find out the last day of the month, calculate the time limit of an activity, searching popular posts within 24 hours, etc, for example:

<?php
echo 'Now: ' . date('d-m-Y H:i:s', strtotime('now'));
echo 'Last date of February 2017: ' . date('d-m-Y', strtotime('last day of february 2017'));
echo 'Max date to submit the report: ' . date('d-m-Y H:i:s', strtotime('3 month 23:59'));
echo 'Due date: ' . date('d-m-Y H:i:s', strtotime('next friday 12:00'));
echo 'Last 24 hours: ' . date('d-m-Y H:i:s', strtotime('-24 hours'));

The result:

Now: 19-02-2017 10:00:56
Last date of February 2017: 28-02-2017
Max date to submit the report: 19-05-2017 23:59:00
Due date: 24-02-2017 12:00:00
Last 24 hours: 18-02-2017 10:00:56

IV. Wrap Up

PHP provides many functions related to date and time, three of which are time(), strtotime() and mktime(), all of them have their own advantages and can be complementary.

Some characteristics:

  • The time() function has no argument and only produce one output that is timestamp of the current time in the UTC time zone (GMT + 0). Date-time manipulation using this function can only be done in seconds.
  • The mktime() function is very similar to the time() function. mktime() is more flexible than time() function because it has more arguments, but date-time manipulation using this function can only be done in date time unit (year, month, day, hour, minute, and second).
  • The strtotime() function is the most flexible because it can calculate time with flexible arguments e.g. counting the next month, the last day of a month, one day ago, etc., while it difficult to calculate using the other functions.

    In addition, this function can also replace the time() function, by giving an argument now: strtotime('now'). It can also replace the mktime() function by giving date time unit as argumen, for example: strtotime(date('Y-m-d H:i:s'));

Based on the above characteristic, always consider using the strtotime() function before using the others.

Related Post

Leave a comment

Like Us

Social