0

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?

Michael
  • 25
  • 2
  • 8
  • 1
    Have you used "$this->format('N') == 6" ? – Dhananjay Kyada Jun 17 '19 at 13:02
  • @Dhananjay Kyada Yes, I have added the above code I found in stackoverflow in the functions.php and I tried what you suggested but still what I get printed from my test is 1 for Mondays – Michael Jun 17 '19 at 13:33
  • Go through https://www.php.net/manual/en/intlcalendar.setfirstdayofweek.php. It may help you. – Dhananjay Kyada Jun 17 '19 at 13:40
  • @Dhananjay Kyada Thanks! Unfortunately it had no effect to my test. – Michael Jun 17 '19 at 14:31
  • IMHO - if you're performing any date related arithmetic in PHP, you really should use a library. It will save you an enormous amount of time and headaches. Currently, https://carbon.nesbot.com/docs/ is the defacto. – waterloomatt Jun 17 '19 at 14:45
  • @waterloomatt The date related arithmetic is in a plugin whose author cannot provide help. He said that I need to change how php’s date function works. – Michael Jun 17 '19 at 15:00

1 Answers1

0
<?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!';
?>
  • Thank you for your suggestion, but when add below your code the: – Michael Oct 28 '20 at 13:13
  • I get altogether: 2020-10-17 is 0th Day of this week! Today's date is: 2020/10/28 The weekday is: Wednesday The number of the weekday is: 3 The week number is:44 – Michael Oct 28 '20 at 13:25
  • But what I need is to have Wednesday taking the weekday number 4 – Michael Oct 28 '20 at 13:26
  • yes because of $standardDate array for this purpose *start day from Monday to Saturday* – Masoud Rahmani Oct 28 '20 at 18:00
  • 0th Day of this week will be 0th of $standardDate array that will be *Sat* and if you want show first day with *1* you can add *+1* to $key and add empty array to $standardDate and it will be: – Masoud Rahmani Oct 28 '20 at 18:03
  • . . $standardDate = array("", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"); . . . echo $date . ' is ' . ($key+1). 'th Day of this week!'; – Masoud Rahmani Oct 28 '20 at 18:04
  • As it is now for php, the first day of the week is Monday. This is why if you just do echo date("N"); on a Monday it returns 1 (on a Sunday returns 7). I do not need to just show something. I want to make calendars, plugins, whatever code, to understand that the first day of the week is Saturday. If succeed then echo date("N"); will return 3 if current the day is Monday. Of course echo date("N"); will return 1 if current day is Saturday and it will return 2 if current day is Sunday. I use echo date("N"); only to see if I have succeeded. – Michael Oct 30 '20 at 10:18
  • This link can help you: https://stackoverflow.com/a/38642109/2489573 – Masoud Rahmani Oct 30 '20 at 12:56
  • Thank you Masoud, but testing all suggestions in the above link with echo date("N"); still doesn't return 1 on Saturdays and 3 on Mondays. – Michael Oct 31 '20 at 14:51