Michael
Michael

Reputation: 25

Change the week start day from Monday to Saturday

To display the number of weekday I use the following:

echo "Today's date is: " . date("Y/m/d") . "<br>";
echo "The weekday is: " . date("l") . "<br>";
echo "The number of the weekday is: " . date("N") . "<br>";
echo "The week number is:" . date("W");

When the day is a Monday the above returns "The number of the weekday is: 1". This is because the week starts on Monday by default. What I need is to change the week start day to Saturday. Then the above "The number of the weekday is: " will return 3 on Mondays and 1 on Saturdays. In this regard the week number in a given date will be affected accordingly.

I have found this code in PHP DateTime() class, change first day of the week to Monday intended to change first day of the week from Sunday to Monday

    class EuroDateTime extends DateTime {


    // Override "modify()"
      public function modify($string) {


      // Change the modifier string if needed
      if ( $this->format('N') == 1 ) { // It's Sunday and we're calculating a day using relative weeks
          $matches = array();
          $pattern = '/this week|next week|previous week|last week/i';
          if ( preg_match( $pattern, $string, $matches )) {
              $string = str_replace($matches[0], '-7 days '.$matches[0], $string);
          }
      }
      return parent::modify($string);

  }

but I have not succeeded in modifying it so to change the week start day to Saturday.

My acid test for what I need to accomplish is particularly the output of:

echo "The number of the weekday is: " . date("N") . "<br>";

Can anyone please suggest the modification needed or another code snippet which will turn the week start day to Saturday?

Upvotes: 0

Views: 1251

Answers (1)

Masoud Rahmani
Masoud Rahmani

Reputation: 25

<?php
    $date = "2020-10-17"; 
    $standardDate = array("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri");
    $dayNameOfWeek = date("D", strtotime($date));
    $key = array_search($dayNameOfWeek, $standardDate);
    echo $date . ' is ' . $key. 'th Day of this week!';
?>

Upvotes: 0

Related Questions