上周,本周 (php)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5710275/
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
last week, this week (php)
提问by TDSii
i am making some statistics, i want to select the time from (last week only) and this week.
我正在做一些统计,我想从(仅上周)和本周中选择时间。
for this week its easy:
本周很简单:
$start = strtotime('this week');
$finish = time();
for last week
上周
$start = strtotime('last week');
$finish = ??????
回答by Jonah
This?
这个?
$start = strtotime('2 weeks ago');
$finish = strtotime('last week');
Edit:change credit to @Dominic Barnes's comment.
编辑:将功劳改为@Dominic Barnes 的评论。
回答by Kalle H. V?ravas
If the question is for statistical PHP script. Then all of the answers and basically the question is wrong.
如果问题是针对统计 PHP 脚本的。那么所有的答案和基本上问题都是错误的。
- Last weekin statistics= One before currently running week from Sunday to Monday
- This weekin statistics= Currently running week from Sunday to Monday
- 上周在统计=一个正在运行的一周,从周日到周一前
- 本周在统计=当前运行的一周,从周日至周一
(or Monday to Sunday, depending on which calendar you are used to, but in PHP that's one week)
(或周一至周日,取决于您习惯的日历,但在 PHP 中为一周)
This means, its not from today minus 7 days. That is not last or this week. So the selected answer currently, is in correct and counts 7 days back. Granted, its Sunday, at the time of testing, so it shows correct. But by editing the now date, you can see the problem:
这意味着,它不是从今天减去 7 天。那不是最后一周或本周。所以当前选择的答案是正确的,并且倒数 7 天。当然,这是测试时的星期天,所以它显示正确。但是通过编辑现在日期,您可以看到问题:
// Selected answer:
$start = strtotime('2 weeks ago');
$finish = strtotime('last week');
// But if today isn't Sunday, you can see the code is wrong:
echo date("d.m.Y", strtotime("1 week ago", strtotime('yesterday')));
// Output: 15.08.2015 00:00:00 - 17.08.2015 00:00:00
You have to set the start of the week and the end of the week. strtotime()
can support more stuff, so you can most likely make this answer better and more neat. However you get the working code and a good example of the logic from...
您必须设置一周的开始和一周的结束。strtotime()
可以支持更多的东西,所以你很可能可以让这个答案更好更整洁。但是,您可以从...中获得工作代码和逻辑的一个很好的示例
My proposed solution:
我提出的解决方案:
$today = strtotime('today 00:00:00');
$this_week_start = strtotime('-1 week monday 00:00:00');
$this_week_end = strtotime('sunday 23:59:59');
$last_week_start = strtotime('-2 week monday 00:00:00');
$last_week_end = strtotime('-1 week sunday 23:59:59');
echo date('d.m.Y H:i:s', $today) . ' - Today for example purposes<br />';
echo date('d.m.Y H:i:s', $this_week_start) . ' - ' . date('d.m.Y H:i:s', $this_week_end) . ' - Currently running week period<br />';
echo date('d.m.Y H:i:s', $last_week_start) . ' - ' . date('d.m.Y H:i:s', $last_week_end) . ' - Last week period<br />';
Above currently produces:
以上目前生产:
30.08.2015 00:00:00 - Today for example purposes
24.08.2015 00:00:00 - 30.08.2015 23:59:59 - Currently running week period
17.08.2015 00:00:00 - 23.08.2015 23:59:59 - Last week period
30.08.2015 00:00:00 - 今天作为示例目的
24.08.2015 00:00:00 - 30.08.2015 23:59:59 - 当前运行的一周时间
17.08.2015 00:00:00 - 205152. :59 - 上周期间
Because for statistics, it has to be accurate and if the end would be 00:00:00, then that date wont be counted in. And if the date would be one day later at 00:00:00, then the date is not correct. There for, this solution is the correct way to do this, for statistical purposes at least.
因为对于统计,它必须是准确的,如果结束是 00:00:00,那么该日期不会被计算在内。如果日期是一天后的 00:00:00,那么该日期不是正确的。因此,至少出于统计目的,此解决方案是执行此操作的正确方法。
回答by alex
回答by krom
If searching for the last week for statistical purposes, starting on Monday, ending on Sunday:
如果出于统计目的搜索上周,从星期一开始,到星期日结束:
$last_week_start = strtotime("monday last week");
$last_week_end = strtotime("monday this week - 1 second");
"this week" is important, otherwise, if this weeks monday is already in the past (e.g. if it is tuesday already), it will give you the monday of next' week.
“本周”很重要,否则,如果本周星期一已经过去(例如,如果已经是星期二),它将为您提供下一周的星期一。
As for the months/years, I used the classy mktime approach:
至于月/年,我使用了经典的 mktime 方法:
last month
上个月
$last_month_start = mktime(0, 0, 0, date('m')-1, 01);
$last_month_end = mktime(23, 59, 59, date('m'), 0);
last year
去年
$last_year_start = mktime(0, 0, 0, 1, 1, date('Y')-1);
$last_year_end = mktime(23, 59, 59, 1, 0, date('Y'));
回答by Baxny
If you are looking for "Last Week" instead of Last 7 days
如果您正在寻找“上周”而不是过去 7 天
$start = strtotime('Last Week'); // Will give you last Monday
$finish = strtotime('Last Sunday'); // Will give you last Sunday