将周开始日期从星期一更改为星期六



为了显示工作日的数量,我使用以下方法:

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");

当一天是星期一时,上面返回"工作日的数字是:1"。这是因为一周默认从星期一开始。我需要的是将一周的开始日更改为星期六。那么上面的"工作日的数字是:"周一返回 3,周六返回 1。在这方面,给定日期的周数将受到相应影响。

我在 PHP DateTime() 类中找到了这段代码,将一周的第一天更改为星期一,目的是将一周的第一天从星期日更改为星期一

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);
}

但我没有成功修改它,以便将一周开始日更改为星期六。

我对我需要完成的事情的酸性测试特别是输出:

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

任何人都可以建议所需的修改或其他代码片段,将一周开始日变成星期六?

<?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!';
?>

相关内容

最新更新