Week list for the current date in PHP

More...

This post shows two examples of how to find the week list for the current date. Input parameter is date, and the output is date list from Monday till Sunday. First example is done with classic algorithm while second example uses ISO week date format.

// set current date
$date = '04/30/2009';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
	print date("m/d/Y l", $ts) . "\n";
}

And here is second example. Just to note that strtotime in PHP 5.1.4 didn't work with YYYY-Www-D format. After I prepared ISO week date format like YYYYWwwD, problem was gone.

// set current date
$date = '04/30/2009';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// find the year (ISO-8601 year number) and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
for($i = 1; $i <= 7; $i++) {
	// timestamp from ISO week date format
	$ts = strtotime($year.'W'.$week.$i);
	print date("m/d/Y l", $ts) . "\n";
}

Here are some examples of ISO week date format:

  • 01/01/2009 is 2009-W01-4 (2009W014)
  • 12/31/2009 is 2009-W53-4 (2009W534)

Each PHP example will output the following list for the input date 04/30/2009

  • 04/27/2009 Monday
  • 04/28/2009 Tuesday
  • 04/29/2009 Wednesday
  • 04/30/2009 Thursday
  • 05/01/2009 Friday
  • 05/02/2009 Saturday
  • 05/03/2009 Sunday
This entry was posted on April 30, 2009 and is filed under PHP

Related posts

24 Responses to Week list for the current date in PHP

  1. dbunic says:

    @Justin Handley - You can prepare first and last dates for the current week and use them as input parameters for querying WordPress posts. Here is how to define dates for Monday and Sunday:

    // set current timestamp
    $today = time();
    // calculate the number of days since Monday
    $dow = date('w', $today);
    $offset = $dow - 1;
    if ($offset < 0) {
        $offset = 6;
    }
    // calculate timestamp for Monday and Sunday
    $monday = $today - ($offset * 86400);
    $sunday = $monday + (6 * 86400);
    // print dates for Monday and Sunday in the current week
    print date("Y-m-d", $monday) . "\n";
    print date("Y-m-d", $sunday) . "\n";
    
  2. anonymous says:

    ThankS!!

  3. SujaArjunan says:

    Hi,
    Can you please help me out to display previous week and next week ?

  4. dbunic says:

    @SujaArjunan - No problem, here is complete solution modified from the first example:

    // set current date
    $date = '10/14/2012';
    // parse about any English textual datetime description into a Unix timestamp
    $ts = strtotime($date);
    // calculate the number of days since Monday
    $dow = date('w', $ts);
    $offset = $dow - 1;
    if ($offset < 0) {
        $offset = 6;
    }
    // calculate timestamp for the Monday
    $ts = $ts - $offset*86400;
    // print current week
    print "Current week\n";
    for ($i = 0; $i < 7; $i++) {
        print date("m/d/Y l", $ts + $i * 86400) . "\n";
    }
    // print previous week
    print "Previous week\n";
    for ($i = -7; $i < 0; $i++) {
        print date("m/d/Y l", $ts + $i * 86400) . "\n";
    }
    // print next week
    print "Next week\n";
    for ($i = 7; $i < 14; $i++) {
        print date("m/d/Y l", $ts + $i * 86400) . "\n";
    }
    

    The trick is to find Monday for the current week and then with loop range fetch previous or next week. Hope this modification will be helpful for your project. Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

In case of posting HTML tags or JavaScript code please convert special characters to HTML entities.
Especially pay attention to convert "<" character to "&lt;" entity!