STRTOTIME to find current week, date for Monday and Saturday, PHP
Asked Answered
O

6

7

I have these lines:

$staticstart = date('Y-m-d',strtotime('last Monday'));
$staticfinish = date('Y-m-d',strtotime('next Saturday'));

And I am using them to select the monday and saturday of the current week, But when it is actually Monday, it choses the monday of the previous week, thus showing 2 weeks of data.... I have tried this and it produces no result:

$staticstart = date('Y-m-d',strtotime('this Monday'));

What have I missed? Is there a better way to find the monday and Saturday (dates) of the current week?

Othello answered 10/12, 2013 at 6:34 Comment(1)
for last monday, a better alternate solution is $last_monday = date('Y-m-d', strtotime("$this_year-W$week_number")); // print date of first day of week (MONDAY), week_number must be two digit and year must be 4 digit ISO 8601Gloaming
P
16

why don't you try like this

//check the current day
if(date('D')!='Mon')
{    
 //take the last monday
  $staticstart = date('Y-m-d',strtotime('last Monday'));    

}else{
    $staticstart = date('Y-m-d');   
}

//always next saturday

if(date('D')!='Sat')
{
    $staticfinish = date('Y-m-d',strtotime('next Saturday'));
}else{

        $staticfinish = date('Y-m-d');
}
Peyton answered 10/12, 2013 at 6:45 Comment(1)
that's perfect, I'm still getting used to the correct way to write php code as I keep swapping between 4 or so languages... One day! Thanks!!Othello
C
6

shortcut:

  $start = (date('D') != 'Mon') ? date('Y-m-d', strtotime('last Monday')) : date('Y-m-d');
  $finish = (date('D') != 'Sat') ? date('Y-m-d', strtotime('next Saturday')) : date('Y-m-d');
Clipper answered 29/4, 2014 at 9:13 Comment(0)
A
4

I think this is a simpler solution

$monday = date('Y-m-d', strtotime('monday this week'));
$saturday = date('Y-m-d', strtotime('saturday this week'));
Aberdare answered 31/1, 2020 at 20:21 Comment(1)
Not sure why this answer was down voted as it is by far the easiest solution to get the dates of days in the current weekKedah
A
2

To get the date range between current week

$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;

$sunday = strtotime(date("Y-m-d",$monday)." +6 days");

$this_week_start = date("Y-m-d",$monday);
$this_week_end = date("Y-m-d",$sunday);

echo "Current week range from $this_week_start to $this_week_end ";
Anyaanyah answered 7/11, 2019 at 13:50 Comment(1)
$monday+7*86400 don't assume that each day has 24 hours :)Debbydebee
J
0
$staticstart = (today == monday ? date('Y-m-d', today) : date('Y-m-d', strtotime('last monday'));
Judaica answered 10/12, 2013 at 6:46 Comment(0)
G
0
      $week_number  = date("W", strtotime('now'));// ISO-8601 week number
      $year_number  = date("o", strtotime('now'));// ISO-8601 year number

      // as per  ISO-8601 specification, December 28th is always in the last week of its year.  The highest week number in a year is either 52 or 53.
      // and January 4th always be in the first week of the year
      // and week 01 is the week with the first Thursday in it 
      // Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week 
      // N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
      // w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)

    // PHP week start from 0 = sunday to 6 saturday 
    // $sunday_last_week = date("Y-m-d", strtotime('sunday last week'));  
    // $sunday_this_week = date("Y-m-d", strtotime('sunday this week'));

    // echo "sunday last week: $sunday_last_week - sunday this week: $sunday_this_week";


    if($week_number<=9)
    {
      $week_number= "0".$week_number; 
    }
    $last_monday = date('Y-m-d', strtotime("$this_year-W$week_number")); 
    // print date of first day of week (MONDAY), week_number must be two digit and year must be 4 digit ISO 8601


    $tuesday= date('Y-m-d',strtotime("$last_monday +1 days"));
    $wednesday= date('Y-m-d',strtotime("$last_monday +2 days"));
    $thursday= date('Y-m-d',strtotime("$last_monday +3 days"));
    $friday= date('Y-m-d',strtotime("$last_monday +4 days"));
    $saturday= date('Y-m-d',strtotime("$last_monday +5 days"));

    $next_sunday= date('Y-m-d',strtotime("$last_monday +6 days"));

  ?>
Gloaming answered 26/6, 2016 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.