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 https://stackoverflow.com/questions/13128854/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.

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

  • 1
    How is this related to WordPress? Looks like it's better suited for Stackoverflow? – birgire Jun 12 at 10:13
0

I think it's not related to WordPress but maybe it can help you:

<?php
ini_set('date.timezone', 'Europe/Lisbon');
ini_set('intl.default_locale', 'es_ES');

$cal = IntlCalendar::createInstance();
$cal->set(2019, 6, 20); // A Saturday

var_dump($cal->getFirstDayOfWeek());

Take a look at: https://www.php.net/manual/en/intlcalendar.setfirstdayofweek.php

  • Thank you for the code. I copied the code into functions.php but the weekday (from the 3rd line of my test code) is still 3 instead of 5 for today Wednesday because the week still starts on Mondays instead of Saturdays. – Michael Jun 12 at 14:15
  • Are you using WordPress? You can simply switch the "Week Starts On" option, going to your Admin Panel -> Settings -> General. – michelmany Jun 13 at 16:23
  • Yes I know about this option in WordPress and I already have set it for Saturday but this has no effect on the date("N") which is my acid test for what I need to accomplish. – Michael Jun 13 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.