Mktime() - Timestamp yesterday, last month, etc.).

December 2016

Timestamps can be very useful in programming and tracking possible modifications which could have been made. The mktime() function in PHP is one such tool which returns UNIX timestamps of a given date. The mktime() function shows the number of seconds passed from 1st January 1970 to a given date. 1st January 1970 is taken to be the starting date for the OS and hence all the timestamp values are generated by keeping this as the base. This can be used to calculate the time elapsed across a wide range of varieties including the last 24 hours, the day before, the current week, the past week, this month, the past month, this year, last year, etc.


Intro


The mktime() function returns of the UNIX timestamp of a given date, i.e. the number of seconds elapsed between 1 January 1970 and that date.

Note: To convert the timestamps below to a format suitable for use with MySQL, use the date function as follows:


<?     
$mysql_datetime = date('Y-m-d H:i:s',$timestamp);     
?>


Below are the mostly used examples:

Last 24 hours


The code below covers the past 24 hours so far:

<?php     
$startTime = mktime() - 24*3600;     
$endTime = mktime();     
?>

Yesterday


The code below works even if you are the 1st of the month or the 1st January of the year. It covers the period from yesterday at 00:00:00 to 23:59:59 yesterday:


<?php     
$startTime = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));     
$endTime = mktime(23, 59, 59, date('m'), date('d')-1, date('Y'));     
?>

This week


The code below assumes that the first day of the week is Monday. It covers the period from Monday morning at 00:00:00 to now:

<?     
$startTime = mktime(0, 0, 0, date('n'), date('j'), date('Y')) - ((date('N')-1)*3600*24);     
$endTime = mktime();      
?>

Last week


The code below assumed that the first day of the week is Monday. It covers the period from the Monday before last at 00:00:00 to the following Sunday at 23:59:59:

<?     
$startTime = mktime(0, 0, 0, date('n'), date('j')-6, date('Y')) - ((date('N'))*3600*24);     
$endTime = mktime(23, 59, 59, date('n'), date('j'), date('Y')) - ((date('N'))*3600*24);     
?>

This Month


The code below covers the period from the first of the current to now:

<?     
$startTime = mktime(0, 0, 0, date('m'), 1, date('Y'));     
$endTime = mktime();     
?>

Last 30 days


The code below covers the period from 30 days ago to now:

<?      
$ starttime = mktime () - 30 * 3600 * 24;      
$ endTime = mktime ();      
> 

Last month


The code below covers the previous month:

<?     
$startTime = mktime() - 30*3600*24;     
$endTime = mktime();     
?> 

Current year


The code below covers the period from January 1st to 00:00:00 today:

<?     
$startTime = mktime(0, 0, 0, 1, 1, date('Y'));     
$endTime = mktime();     
?>

Last year


The code below covers the previous year, from January 1, at 00:00:00 to 31 December at 23:59:59:

<?     
$startTime = mktime(0, 0, 0, 1, 1, date('Y')-1);     
$endTime = mktime(23, 59, 59, 12, 31, date('Y')-1);     
?>

Related :

This document entitled « Mktime() - Timestamp yesterday, last month, etc.). » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.