php strtotime“上个星期一”如果今天是星期一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4150435/
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 strtotime "last monday" if today is monday?
提问by Nathan H
I want to use strtotime("last Monday")
.
我想用strtotime("last Monday")
.
The thing is, if today IS MONDAY, what does it return? It seems to be returning the date for the monday of last week. How can I make it return today's date in that case?
问题是,如果今天是星期一,它会返回什么?它似乎正在返回上周星期一的日期。在这种情况下,我怎样才能让它返回今天的日期?
回答by jmlnik
If you read the manual, there is an great example that describes exactly what you want to do http://www.php.net/manual/en/datetime.formats.relative.php
如果你阅读手册,有一个很好的例子,它准确地描述了你想要做什么http://www.php.net/manual/en/datetime.formats.relative.php
strtotime('Monday this week');
Update:There appears to be a bugintroduced in newer versions of PHP where this week
returns the wrong week when ran on Sundays. You can vote on the bug here: https://bugs.php.net/bug.php?id=63740
更新:似乎在较新版本的 PHP 中引入了一个错误,this week
在星期日运行时返回错误的一周。您可以在此处对错误进行投票:https: //bugs.php.net/bug.php?id=63740
Update 2:As of May 18th 2016, this has been fixed in PHP 5.6.22, PHP 7.0.7 and PHP 7.1-dev (and hopefully remains fixed in subsequent releases) as seen here: https://bugs.php.net/bug.php?id=63740#1463570467
更新 2:截至 2016 年 5 月 18 日,此问题已在 PHP 5.6.22、PHP 7.0.7 和 PHP 7.1-dev 中修复(希望在后续版本中保持修复),如下所示:https: //bugs.php.net /bug.php?id=63740#1463570467
回答by zerkms
How can I make it return today's date in that case?
在这种情况下,我怎样才能让它返回今天的日期?
pseudocode:
伪代码:
if (today == monday)
return today;
else
return strtotime(...);
Btw, this trick also could work:
顺便说一句,这个技巧也可以工作:
strtotime('last monday', strtotime('tomorrow'));
回答by Vic
If today is Monday, strtotime("last Monday") will return a date 7 days in the past. Why don't you just check if today is Monday and if yes, return today's date and if not, return last week?
如果今天是星期一, strtotime("last Monday") 将返回过去 7 天的日期。你为什么不检查今天是否是星期一,如果是,返回今天的日期,如果不是,返回上周?
That would be a foolproof way of doing this.
这将是一种万无一失的方法。
if (date('N', time()) == 1) return date('Y-m-d');
else return date('Y-m-d', strtotime('last Monday'));
回答by Farside
As it was correctly outlined in the previous answer, this trick works, but also had caveatsprior to PHP 5.6.22
, PHP 7.0.7
and PHP 7.1-dev
:
正如在上一个答案中正确概述的那样,此技巧有效,但在,和之前也有警告:PHP 5.6.22
PHP 7.0.7
PHP 7.1-dev
strtotime('last monday', strtotime('tomorrow'));
// or this one, which is shorter, but was buggy:
strtotime('Monday this week');
To those, who prefer the "Jedy-way", to work with objects of the DateTime
class, the solution is next:
对于那些,谁喜欢“Jedy路”,与对象工作的DateTime
类,该解决方案是未来:
(new \DateTime())->modify('tomorrow')->modify('previous monday')->format('Y-m-d');
or even shorter notation:
甚至更短的符号:
\DateTime('Monday this week')
Be carefull, because if you do the same on SQL, you don't need to have any of these tricks in mysqlwith addition of "tomorrow". Here's how the solution will look:
小心,因为如果你在 SQL 上做同样的事情,你不需要在mysql 中使用任何这些技巧并添加“tomorrow”。以下是解决方案的外观:
SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) as last_monday;
回答by Chris Baker
Late answer, but I thought I would post up this answer (which is actually from a different but related question). It handles the scenario in the question:
迟到的答案,但我想我会发布这个答案(实际上来自一个不同但相关的问题)。它处理问题中的场景:
function last_monday($date) {
if (!is_numeric($date))
$date = strtotime($date);
if (date('w', $date) == 1)
return $date;
else
return strtotime(
'last monday',
$date
);
}
echo date('m/d/y', last_monday('8/14/2012')); // 8/13/2012 (tuesday gives us the previous monday)
echo date('m/d/y', last_monday('8/13/2012')); // 8/13/2012 (monday throws back that day)
echo date('m/d/y', last_monday('8/12/2012')); // 8/06/2012 (sunday goes to previous week)
try it: http://codepad.org/rDAI4Scr
试试看:http: //codepad.org/rDAI4Scr
... or a variation that has sunday return the following day (monday) rather than the previous week, simply add a line:
...或者星期日返回第二天(星期一)而不是前一周的变体,只需添加一行:
elseif (date('w', $date) == 0)
return strtotime(
'next monday',
$date
);
try it: http://codepad.org/S2NhrU2Z
试试看:http: //codepad.org/S2NhrU2Z
You can pass it a timestamp or a string, you'll get back a timestamp
你可以传递一个时间戳或一个字符串,你会得到一个时间戳
Documentation
文档
回答by Johno
Depending on exactly what you're using it for, this may be useful. Since one second's ambiguity is OK for my requirements, I use:
根据您使用它的确切目的,这可能很有用。由于一秒钟的歧义可以满足我的要求,我使用:
date( 'Y-m-d 23:59:59', strtotime( 'last sunday' ))
to get midnight on the most recent Monday (or today if today IS Monday).
在最近的星期一(或今天,如果今天是星期一)获得午夜。
回答by carbonero
My aproach:
我的方法:
date_default_timezone_set('Europe/Berlin');
function givedate($weekday, $time) {
$now = time();
$last = strtotime("$weekday this week $time");
$next = strtotime("next $weekday $time");
if($now > $last) {
$date = date("d.m.Y - H:i",$next);
}
else {
$date = date("d.m.Y - H:i",$last);
}
return $date;
}
echo givedate('Wednesday', '00:52');
Or monthly
或每月
function givedate_monthly($weekday, $time) {
$now = time();
$last = strtotime("first $weekday of this month $time");
$next = strtotime("first $weekday of next month $time");
if($now > $last) {
$date = date("d.m.Y - H:i",$next);
}
else {
$date = date("d.m.Y - H:i",$last);
}
return $date;
}
echo givedate_monthly('Wednesday', '01:50');
回答by Stefan Gabos
$monday = strtotime('Monday last week');
$sunday = strtotime('+6 days', $monday);