43

我想用strtotime("last Monday").

问题是,如果今天是星期一,它会返回什么?它似乎正在返回上周星期一的日期。在这种情况下,我怎样才能让它返回今天的日期?

4

8 回答 8

142

如果您阅读手册,有一个很好的示例可以准确描述您想要做什么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

于 2012-01-24T03:41:28.540 回答
74

在这种情况下,我怎样才能让它返回今天的日期?

伪代码:

if (today == monday)
    return today;
else
    return strtotime(...);

顺便说一句,这个技巧也可以工作:

strtotime('last monday', strtotime('tomorrow'));
于 2010-11-11T00:43:00.563 回答
5

如果今天是星期一,strtotime("last Monday") 将返回过去 7 天的日期。你为什么不检查今天是否是星期一,如果是,返回今天的日期,如果不是,返回上周?

这将是一种万无一失的方法。

if (date('N', time()) == 1) return date('Y-m-d');
else return date('Y-m-d', strtotime('last Monday'));

http://us2.php.net/manual/en/function.date.php

于 2010-11-11T00:46:15.520 回答
3

正如上一个答案中正确概述的那样,这个技巧有效,但在和之前也有警告PHP 5.6.22PHP 7.0.7PHP 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 上做同样的事情,你不需要在中添加“明天”来使用这些技巧。以下是解决方案的外观:

SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) as last_monday;
于 2017-02-20T10:12:08.987 回答
1

迟到的答案,但我想我会发布这个答案(这实际上来自一个不同但相关的问题)。它处理问题中的场景:

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

你可以传递一个时间戳或一个字符串,你会得到一个时间戳

文档

于 2012-08-02T05:00:06.787 回答
0

根据您使用它的确切用途,这可能很有用。由于一秒钟的模糊性可以满足我的要求,因此我使用:

date( 'Y-m-d 23:59:59', strtotime( 'last sunday' ))

在最近的星期一(如果今天是星期一,则为今天)获得午夜。

于 2013-02-01T12:47:20.090 回答
0

我的方法:

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');
于 2013-08-06T23:08:09.193 回答
-1
$monday = strtotime('Monday last week');
$sunday = strtotime('+6 days', $monday);
于 2012-12-12T08:51:38.017 回答