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

Last update on March 9, 2009 07:24 AM by jak58
Published by jak58

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





Intro


The mktime()function allows the return of the UNIX timestamp of a given date, ie the number of seconds elapsed between 1 January 1970 and a specific date.

However, in its current usage, it is usually necessary to define two timestamps for a certain period.

Note that to convert the timestamps below dated MySQL (DATETIME), just 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 is assumed 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 is assumed that the first day of the week is Monday. It covers the period from Monday(15 day before) at 00:00:00 to next 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 1 month to now (current month):

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

Last 30 days (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 period from last month:

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

Current year(this year)


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

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

Last year (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);
?>
Best answers for « Mktime() Timestamp yesterday, last month, etc.). » in :
MySQL - SELECT ... YESTERDAY Show MySQL - SELECT ... YESTERDAY Make use of the following syntax: SELECT * FROM myTable WHERE field_date = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY) In order to select previous records: SELECT * FROM myTable WHERE field_date =...
[PHP]Last day of the month/Number of days in the month Show Last day of the month and number of days in a month Using $m as the number of the month i and $y as year. Function date() can display directly the number of days in the month using the "t" character:
How to display the last day of the month on your website? ShowHow to display the last day of the month on your website? There is a very easy way to display the last day of the month on your website with a PHP code that you will add in your document. All you have to do is to add “$m” to refer to...
Verifying date format in PHP ShowVerifying date format in PHP There are several ways to check the format of a date in PHP; the simplest method is to make use regular expressions. To confirm a date format DD/MM/YYYY, where the days and/or month can be given as...
How to Create a date from 3 different cells? ShowHow to Create a date from 3 different cells? Issue Solution Note Issue In excel I have date spread across 3 cells; 1 with the day (A1), 1 with the month (A2) and 1 with the year (A3). I want to put these 3 numbers into one cell as a...
Download TimeStamp ShowThis small program will interest you. TimeStamp is edited by Orange Lamp Software to help you to remind you to go home after a long day of hard labor. Well, yes, some people need this type of software. It can export your data into text or Excel...