mktime() First Day of the Last Week

The mktime() function returns the Unix timestamp for a date.
This timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Assumed Moday is the firstday of the week.

<?php

//YESTERDAY
$yesterday = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));

//THIS WEEK
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("n"), date("j"), date("Y")) - ((date("N")-1)*3600*24));
$toDate = CURRENT_TIMESTAMP;
//LAST WEEK
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("n"), date("j")-6, date("Y")) - ((date("N"))*3600*24));
$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, date("n"), date("j"), date("Y")) - ((date("N"))*3600*24));


//THIS MONTH
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")  , 1, date("Y")));
$toDate = CURRENT_TIMESTAMP;

//LAST MONTH
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")-1  , 1 , date("Y")));
$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, date("m"), date("d")-date("j"), date("Y")));

//THIS YEAR
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date(1)  , date(1), date("Y")));
$toDate = CURRENT_TIMESTAMP;

//LAST YEAR
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, 1  , 1, date("Y")-1));
$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, 12, 31, date("Y")-1));

?>

About Tanveer
RIA (Rich Internet Application) Developer with a wide variety of business applications. Particularly interested in client/server and relational database design. Always interested in migration projects, as well as close interaction with the DB manufacturers.

2 Responses to mktime() First Day of the Last Week

  1. Alan Ihre says:

    Thanks, this really helped me! :D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 286 other followers