Get Timestamps using MkTime in PHP

in PHP/Tutorials & Samples

PHP Timestamps can be very useful in programming and tracking possible modifications which could have been made. The mktime() function in PHP is a powerful tool that 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, yesterday, last week, this week, last month, etc…

Common Mktime() Timestamps

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:

<?php     

$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();     

?>

Today

The code below covers today:

<?php     

$startTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));  

$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:

<?     

$m = date('n');

$startTime = mktime(0,0,0,$m-1,1,date('Y')); 

$endTime = mktime(23,59,59,$m,0,date('Y'));    

?> 

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);     

?>
Tags:

Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

Leave a Reply

Your email address will not be published.

*

Latest from PHP

Go to Top