[英]php strtotime “last monday” if today is monday?
我想使用strtotime("last Monday")
。
问题是,如果今天是星期一,那又回归了什么? 它似乎正在返回上周一的星期日。 在这种情况下,如何让它返回今天的日期?
如果您阅读本手册,有一个很好的例子可以准确描述您想要做什么http://www.php.net/manual/en/datetime.formats.relative.php
strtotime('Monday this week');
更新:似乎在较新版本的PHP中引入了一个错误 , this week
在周日运行时返回错误的一周。 您可以在此处对错误进行投票: https : //bugs.php.net/bug.php?id = 63740
更新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
在这种情况下,如何让它返回今天的日期?
伪代码:
if (today == monday)
return today;
else
return strtotime(...);
顺便说一下,这个技巧也可行:
strtotime('last monday', strtotime('tomorrow'));
如果今天是星期一,则strtotime(“上周一”)将返回过去7天的日期。 你为什么不检查今天是否是星期一,如果是,请返回今天的日期,如果没有,请回到上周?
这样做是一种万无一失的方式。
if (date('N', time()) == 1) return date('Y-m-d');
else return date('Y-m-d', strtotime('last Monday'));
正如前面的答案中正确概述的那样 ,这个技巧有效,但在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');
对于那些喜欢“Jedy-way”的人来说,使用DateTime
类的对象,接下来是解决方案:
(new \DateTime())->modify('tomorrow')->modify('previous monday')->format('Y-m-d');
甚至更短的符号:
\DateTime('Monday this week')
要小心,因为如果你在SQL上做同样的事情,你不需要在mysql中添加任何这些技巧,并添加“明天”。 以下是解决方案的外观:
SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) as last_monday;
迟到的答案,但我想我会发布这个答案(这实际上来自一个不同但相关的问题)。 它处理问题中的场景:
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)
试试吧: http : //codepad.org/rDAI4Scr
......或者是在第二天(星期一)而不是前一周星期日返回的变体,只需添加一行:
elseif (date('w', $date) == 0)
return strtotime(
'next monday',
$date
);
试试吧: http : //codepad.org/S2NhrU2Z
你可以传递时间戳或字符串,你会得到一个时间戳
文档
根据您使用它的确切内容,这可能很有用。 由于一秒的模糊性可以满足我的要求,我使用:
date( 'Y-m-d 23:59:59', strtotime( 'last sunday' ))
在最近的星期一(或今天如果是星期一)获得午夜。
我的方法:
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');
或者每月
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');
$monday = strtotime('Monday last week');
$sunday = strtotime('+6 days', $monday);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.