WPDKDateTime::daysToWeekStart PHP Method

daysToWeekStart() public static method

Returns number of days to start of this week.
Author: =stid= ([email protected])
public static daysToWeekStart ( object $date, string $first_day = 'monday' ) : integer
$date object Date object
$first_day string
return integer $date
    public static function daysToWeekStart($date, $first_day = 'monday')
    {
        $week_days = array('monday' => 0, 'tuesday' => 1, 'wednesday' => 2, 'thursday' => 3, 'friday' => 4, 'saturday' => 5, 'sunday' => 6);
        $start_day_number = $week_days[$first_day];
        $wday = $date->format("w");
        $current_day_number = $wday != 0 ? $wday - 1 : 6;
        return WPDKMath::rModulus($current_day_number - $start_day_number, 7);
    }

Usage Example

Example #1
0
 /**
  * Returns number of days to start of this week.
  *
  * @author     =stid= <*****@*****.**>
  *
  * @param string $date
  * @param string $first_day Optional. Start from `monday`
  *
  * @return     int
  */
 public static function beginningOfWeek($date, $first_day = 'monday')
 {
     $days_to_start = WPDKDateTime::daysToWeekStart($date, $first_day);
     return $date - $days_to_start;
 }