Monday, 24 November 2014

PHP Mktime() - Timestamp yesterday, last month, 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. 

Syntax:
mktime(hour, minute, second, month, day, year, is_dst) 

Parameter

NameDescriptionRequired/
Optional
Type
hourNumeric representation of hoursOptionalInteger
minuteNumeric representation of minuteOptionalInteger
secondNumeric representation of secondOptionalInteger
monthNumeric representation of monthOptionalInteger
dayNumeric representation of dayOptionalInteger
yearNumeric representation of yearOptionalInteger
is_dstWhether Daylight Saving Time is active (values 1 or 0 ). In PHP 5.1.0, this parameter became deprecated. As a result, the new timezone handling features should be used instead.OptionalInteger

Return value

The Unix timestamp corresponding to the parameters given. If the parameters are invalid, the function returns FALSE.
Value Type : Integer.


Example :

  1. <?php  
  2. echo(date("M-d-Y",mktime(0,0,0,2,12,98))."<br />");  
  3. echo(date("M-d-Y",mktime(0,0,0,17,4,2005))."<br />");  
  4. echo(date("M-d-Y",mktime(0,0,0,1,36,2000))."<br />");  
  5. echo(date("M-d-Y",mktime(24,0,0,1,1,99))."<br />");  
  6. ?>  

Output :

Feb-12-1998
May-04-2006
Feb-05-2000
Jan-02-1999
- See more at: http://www.w3resource.com/php/function-reference/mktime.php#sthash.1FAVbWEq.dpuf

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

0 comments:

Post a Comment