Week list for the current date in PHP

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

41 thoughts on “Week list for the current date in PHP”

  1. @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. @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!

  3. Hello. This guide fits exactly to my project for school canteen that will list this week and next week’s menus. Thank you.

    Can you show me how I can pull past / current / next week’s issue out?

    Thanks in advance.

  4. @Allan – Previous week and next week are shown in my comment above. You can see how loop bounds are set for previous, current and next week.

  5. i have been used this coding on php..and it working good.. but can i use it in jsp?

  6. @Ain – PHP code can’t be directly used for JSP page. If you understand presented logic in PHP, it will be easy for you to “translate” PHP to Java (or JSP).

  7. @Aroune – If you want to start week with Sunday, then make the following change in first script:

    ...
    // calculate the number of days since Sunday
    $dow = date('w', $ts);
    $offset = $dow;
    ...
    

    In other words, $dow (day of week) is equal to offset.

  8. @Chinmoy das – Here is script modification:

    // set start date
    $date = '10/15/2014 12:00';
    // parse about any English textual datetime description into a Unix timestamp
    $ts = strtotime($date);
    // loop through next three months (90 days)
    for ($i = 0; $i < 90; $i++, $ts += 86400) {
        // if day is sunday, then print it out
        if (date('w', $ts) == 0) {
            print date("m/d/Y l", $ts) . "\n";
        }
    }
    

    … and the result is:

    10/19/2014 Sunday
    10/26/2014 Sunday
    11/02/2014 Sunday
    11/09/2014 Sunday
    11/16/2014 Sunday
    11/23/2014 Sunday
    11/30/2014 Sunday
    12/07/2014 Sunday
    12/14/2014 Sunday
    12/21/2014 Sunday
    12/28/2014 Sunday
    01/04/2015 Sunday
    01/11/2015 Sunday
    
  9. Hi, can you show how to display day in a week by pick up a date only? Thanks in advance.

  10. @NANA – Here is PHP code that will print day in a week for some date:

    // set date
    $date = '5/25/2015 12:00';
    // parse about any English textual datetime description into a Unix timestamp
    $ts = strtotime($date);
    // print day in a week
    print date('l', $ts);
    

    … and the result is “Monday”.

  11. Hi dbunic,

    Thanks for your code. It’s amazing.

    I have a table with records for an entire month. On the form, the user enters start_date and end_date.

    I want to display the records in the table based on the start_date and end_dates, but the records should be broken down into weeks from Monday – Sunday. Below is my code:

    
    // selecting records from the OT table
    $query_3 = "SELECT id, name,
                CONVERT(varchar(20), TIMESTAMP, 120) AS TIMESTAMP,
                hrs,
                COMMENT
                FROM salaried_OT
                WHERE name = '" . $valid_user . "' AND
                      TIMESTAMP BETWEEN CONVERT(datetime, '$start_date', 120) AND
                      CONVERT(datetime, '$end_date', 120)
                ORDER BY TIMESTAMP ASC";
    
    $params_3 = array($_REQUEST['query_3']);
    $result_3 = sqlsrv_query($conn, $query_3, $params_3);
    $numFields = sqlsrv_num_fields($result_3);
    
    // open loop
    for ($i = 0; $i < $numFields; $i += 7) { 
        while( $row = sqlsrv_fetch_array($result_3, SQLSRV_FETCH_ASSOC)) {
            // Iterate through the fields of each row.
            echo "";
            echo '' . strip_tags($row['id']) . '';
            echo '' . date( 'd M Y', strtotime(strip_tags($row['timestamp']))) . "";
            echo '' . date( 'W', strtotime(strip_tags($row['timestamp']))) . "";
            echo '' . date( 'l', strtotime(strip_tags($row['timestamp']))) . "";
            echo '' . strip_tags($row['name']) . "";
            echo "" . strip_tags($row['hrs']) . "";
            echo "" . strip_tags($row['comment']) . "";
            echo 'Edit/Delete';
            echo "";
            echo ""; 
        }
        echo "";  
    }
    

    Thanks in advance.

  12. @Luggy – I had similar request to create calendar in upper right corner of the TV schedule site:

    http://raspored.hrt.hr/

    The key is to determine day of week for the first entry and based on that info calculate offset. Let’s say that first entry in your dataset is Wednesday. In this case your offset is 2 and you’ll have empty cells for Monday and Tuesday. After that you can freely fill each cell in chunks of 7 days.

    Hope this tip will help you to create PHP logic for your project.

  13. Thank you @dbunic for your response.

    Unfortunately, I tried it and the result was empty. No record is displayed from the database.

  14. Hello, this is a very useful article. @dbunic Thanks a lot for your contribuition, it’s been very helpful. I modified it to my needs. This particular version, returns an array of the dates of the current week, very useful for queries.

    function current_week()
    {
    // set current timestamp
    $today = time();
    $w = array();
    // calculate the number of days since Monday
    $dow = date('w', $today);
    $offset = $dow - 1;
    if ($offset < 0) {
    $offset = 6;
    }
    // calculate timestamp from Monday to Sunday
    $monday = $today - ($offset * 86400);
    $tuesday = $monday + (1 * 86400);
    $wednesday = $monday + (2 * 86400);
    $thursday = $monday + (3 * 86400);
    $friday = $monday + (4 * 86400);
    $saturday = $monday + (5 * 86400);
    $sunday = $monday + (6 * 86400);

    // return current week array
    $w['monday '] = $monday ;
    $w['tuesday '] = $tuesday ;
    $w['wednesday'] = $wednesday;
    $w['thursday '] = $thursday ;
    $w['friday '] = $friday ;
    $w['saturday '] = $saturday ;
    $w['sunday '] = $sunday ;
    }

    Again, thanks a lot.

Leave a Comment