PHP:一周从星期一开始,但星期日的“本周星期一”到下周星期一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13798026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Week starts on Monday, but "monday this week" on a Sunday gets Monday next week
提问by tvanc
Here's a summary of the issue: On Sundays, strtotime('this week')returns the start of next week.
这是问题的摘要:在星期日,strtotime('this week')返回下周的开始。
In PHP, the week seems to start on Monday. But, on any day except Sunday, this code
在 PHP 中,一周似乎从星期一开始。但是,在星期日以外的任何一天,此代码
echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));
Outputs the date of this week's Monday, when it seems like it should be outputting last weeks Monday. It seems like, in this case, PHP is treating both Sunday and Monday as the the start of the week. It's now Monday, Dec 10, 2012, or 2012-12-10. date('Y-m-d', strtotime('sunday last week'))returns 2012-12-09- yesterday.
输出本周星期一的日期,当它看起来应该输出上周星期一时。在这种情况下,PHP 似乎将星期日和星期一都视为一周的开始。现在是 2012 年 12 月 10 日星期一或 2012-12-10。date('Y-m-d', strtotime('sunday last week'))返回2012-12-09- 昨天。
Is this a bug, or am I missing something? It seems like a bug this obvious should be fairly well known, but I can't find anything about it. Is the only way to get the start of the week to use some special handling for Sundays?
这是一个错误,还是我错过了什么?似乎这个明显的错误应该是众所周知的,但我找不到任何关于它的信息。在星期天使用一些特殊处理是让一周开始的唯一方法吗?
$week_offset = (int) 'sunday' == date('l');
$week_start = strtotime("-$week_offset monday"); // 1 or 0 Mondays ago
采纳答案by tvanc
As far as I can tell, this is a bug. I see no logical reason why strtotime('this week');should return a future date. This is a pretty major bug. In my particular case, I had a leaderboard that showed the users with the most points since the beginning of the week. But on Sundays, it was empty because strtotimereturned a timestamp for a future date. I was doubtful, because just I don't know how this could have gone unnoticed, but I couldn't find any other reports of this bug.
据我所知,这是一个错误。我看不出为什么strtotime('this week');应该返回未来日期的合乎逻辑的理由。这是一个相当大的错误。在我的特殊情况下,我有一个排行榜,显示自本周开始以来得分最高的用户。但是在星期日,它是空的,因为strtotime返回了未来日期的时间戳。我很怀疑,因为我不知道这怎么可能会被忽视,但我找不到关于此错误的任何其他报告。
Thanks for all your time and help, folks.
感谢您的所有时间和帮助,伙计们。
回答by Vadim Guzev
Here is how you can get Monday of current week:
以下是获取本周星期一的方法:
echo date("Y-m-d", strtotime(date('o-\WW')));
回答by Bryant
This answer is late, but it's something that I've been struggling with. Every solution I've tried so far has malfunctioned for one reason or another. This is what I ended up with that worked for me. (though it may be look pretty, it at least works).
这个答案来晚了,但这是我一直在努力解决的问题。到目前为止,我尝试过的每种解决方案都因某种原因而出现故障。这就是我最终对我有用的东西。(虽然它可能看起来很漂亮,但它至少有效)。
$thisMonday = strtotime('next Monday -1 week', strtotime('this sunday'));
回答by sebtucknott
It's not ideal but this is what I resorted to using:
这并不理想,但这是我使用的方法:
if(date('N') == 7) {
$date = date('Y-m-d',strtotime('monday last week'));
} else {
$date = date('Y-m-d',strtotime('monday this week'));
}
回答by Nemo
I think the only problem with your coding is TimeZone.
Solution:
Set your own time Zone. Here is the example of my own time zone:
解决方案:
设置您自己的时区。这是我自己的时区示例:
Example
例子
date_default_timezone_set('Asia/Kolkata');
Set the above line before calling any time function.
Have a nice day.
祝你今天过得愉快。
回答by Baim Wrong
回答by Overcomer
This is for thos looking for a friendly solution that works with any day.
这是为那些正在寻找适用于任何一天的友好解决方案的人而设计的。
function getWeekStart($week_start_day = "Monday") {
$week_days = array("Sunday"=>0,"Monday"=>1,"Tuesday"=>2,"Wednesday"=>3,"Thursday"=>4,"Friday"=>5,"Saturday"=>6,);
if(!isset($week_days[$week_start_day])) {
return false;
} else {
$start_day = $week_days[$week_start_day];
$today = date("w");
$one_day = (60 * 60 * 24);
if($today < $start_day) {
$days_difference = 7 - ($start_day - $today);
} else {
$days_difference = ($today - $start_day);
}
$week_starts = strtotime(date("Y-m-d 00:00:00")) - ($one_day * $days_difference);
return $week_starts;
}
}
//Test: If today is Monday, it will return today's date
echo date("Y-m-d H:i:s", getWeekStart("Monday"));
回答by chasepeeler
If you want the most recent monday:
如果您想要最近的星期一:
function mostRecentMonday(){
if(date("w") == 1){
return strtotime("midnight today");
} else {
return strtotime("last monday");
}
}
Easy to modify to use DateTime, or, to even specify a different date to use as the base.
易于修改以使用 DateTime,或者甚至可以指定不同的日期作为基准。
回答by anuj arora
I think instead of trying
我认为而不是尝试
echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));
you should try
你应该试试
echo date('Y-m-d', strtotime('monday last week'));
回答by anuj arora
Try this code
试试这个代码
// set current date
$date = date("m/d/Y");
$ts = strtotime($date); // also $ts = time();
// find the year and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
$i = 1; // 1 denotes the first day of week
$ts = strtotime($year.'W'.$week.$i);
echo $day = date("l", $ts); // generate the name of day
echo "<br>";
echo $day = date("Y-m-d", $ts); // generate the date
You will get the the date of current week, whether you are on monday you will get the date of that monday.
您将获得本周的日期,无论您是在星期一,您都将获得该星期一的日期。

