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

18 Responses to Week list for the current date in PHP

  1. Emil Bryggare says:

    Thanks, exactly what I was looking for!

  2. bhanu says:

    Thanks. Its Really good work

  3. Swetz says:

    Hey its really nice one.. exactly i required the same... good job..
    thanks

  4. Tushar Mahajan says:

    Hey Thanks Man, Nice Work .

  5. hello says:

    Not a Proper Working

  6. dbunic says:

    @hello - This PHP function is used in production for a while and it works smoothly. I would gladly fix the problem if you can give more details. Thanks!

  7. Ali says:

    NICE! Thanks this is really handy. I wonder, how would i get this to begin not at monday but at the day it is today and then list the next 6 days.

  8. samkrish says:

    thanks for giving the logic.it's really working properly.....
    i got the determine result..

  9. Stefan says:

    thanks for this snippet! saved my week.. ;)

  10. santhosh says:

    yes it's working thanks a lot

  11. santhosh says:
    $date = date('m/d/Y');
    $ts = strtotime($date);
    $dow = date('w', $ts);
    $offset = $dow - 1;
    if ($offset < 0) {
        $offset = 6;
    }
    $ts = $ts - $offset*86400;
    for ($i = 0; $i < 7; $i++, $ts += 86400) {
        $data .= date(" d", $ts);
    }
    $pieces = explode(" ", $data);
    
  12. Dean says:

    How can I display just one date for a chosen day?

  13. dbunic says:

    @Dean - This script will return whole week from Monday till Sunday for a given date. If you want to display (say Wednesday) it is enough to define $i=3 and to comment out the for loop. Something like this for the second example:

    $i = 3;
    // 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";
    //}
    
  14. Dean says:

    Thanks. What I am trying to do is detect the current date, then display the days and dates for Monday through to Sunday for a timetable. I'm a bit noob. :)

  15. dbunic says:

    @Dean - OK, here is modified PHP code that will display current week and mark current date.

    $format = 'm/d/Y l';         // set Date format
    $ts = time();                // set current Unix timestamp
    $today = date($format, $ts); // set today
    // 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);
        // set day
        $day = date($format, $ts);
        // test if $day is $today
        if ($day == $today) {
            $flag = ' [today]';
        }
        else {
            $flag = '';
        }
        // display result
        print $day . $flag . "\n";
    }
    

    ... and the result will be:

    06/13/2011 Monday
    06/14/2011 Tuesday
    06/15/2011 Wednesday [today]
    06/16/2011 Thursday
    06/17/2011 Friday
    06/18/2011 Saturday
    06/19/2011 Sunday
    
  16. Nadeem says:

    Thanx its Really Helpfull

  17. waseem says:

    NICE !!!!!!!!!!!! This is a very beautiful site.
    my every problem solve about PHP to read tutorial

  18. Jag says:

    Hi there im trying to display next week using same principle but it doesnt seem to be working :S..i have used

    $nextweek=date("d m Y", strtotime("next Week"));

    and same structure as above but does not work.

    any suggestion on how i can print next week ??

    Thanks

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>