上周,本周 (php)
我正在做一些统计,我想选择(仅限上周)和本周的时间。
本周很简单:
$start = strtotime('this week');
$finish = time();
上周
$start = strtotime('last week');
$finish = ??????
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 = ??????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这?
编辑:将信用更改为@Dominic Barnes 的评论。
This?
Edit: change credit to @Dominic Barnes's comment.
如果问题是用于统计PHP脚本。那么所有的答案和基本上问题都是错误的。
(或星期一到星期日,具体取决于您习惯的日历,但在 PHP 中是一周)
这意味着,它不是从今天减去 7 天。这不是上周或本周。因此,当前选择的答案是正确的,并且可以追溯到 7 天之前。当然,测试时是周日,所以它显示正确。但通过编辑现在的日期,您可以看到问题:
您必须设置一周的开始和一周的结束。
strtotime()
可以支持更多内容,因此您很可能可以使这个答案更好、更简洁。但是,您可以从以下位置获得工作代码和一个很好的逻辑示例:我提出的解决方案:
以上当前产生:
因为对于统计来说,它必须准确,如果结束时间是 00:00:00,那么该日期将不会被计算在内。如果日期是一天后的 00:00:00,则日期不正确。因此,至少出于统计目的,该解决方案是执行此操作的正确方法。
If the question is for statistical PHP script. Then all of the answers and basically the question is wrong.
(or Monday to Sunday, depending on which calendar you are used to, but in PHP that's one week)
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:
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...My proposed solution:
Above currently produces:
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.
这就是你想要的吗?
Dominic 还指出
time() === strtotime('this week')
(CodePad)。Is that what you want?
Dominic also points out that
time() === strtotime('this week')
(CodePad).如果出于统计目的搜索上周,从星期一开始,到星期日结束:
“本周”很重要,否则,如果本周星期一已经过去(例如,如果已经是星期二),它将为您提供下周的星期一。
至于月份/年份,我使用了经典的 mktime 方法:
上个月
去年
If searching for the last week for statistical purposes, starting on Monday, ending on Sunday:
"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:
last month
last year
如果您正在寻找“上周”而不是过去 7 天
If you are looking for "Last Week" instead of Last 7 days