代码之家  ›  专栏  ›  技术社区  ›  Nathan H

php strtotime“上星期一”如果今天是星期一?

  •  35
  • Nathan H  · 技术社区  · 15 年前

    我想用 strtotime("last Monday")

    问题是,如果今天是星期一,会有什么回报?

    8 回复  |  直到 15 年前
        1
  •  70
  •   zerkms    15 年前

    如果那样的话,我怎么能把今天的日期还回来呢?

    伪码:

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

    strtotime('last monday', strtotime('tomorrow'));
    
        2
  •  121
  •   jmlnik    9 年前

    如果你阅读了手册,有一个很好的例子可以准确地描述你想做什么 http://www.php.net/manual/en/datetime.formats.relative.php

    strtotime('Monday this week');
    

    更新: 缺陷 this week 星期天跑步时返回错误的一周。你可以在这里投票: https://bugs.php.net/bug.php?id=63740

    更新2: 截至2016年5月18日,已在PHP 5.6.22、PHP 7.0.7和php7.1-dev中修复了此问题(希望在后续版本中保持修复),如下所示: https://bugs.php.net/bug.php?id=63740#1463570467

        3
  •  5
  •   Vic    15 年前

    如果今天是星期一,strtotime(“上星期一”)将返回过去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

        4
  •  2
  •   Community Mohan Dere    8 年前

    previous answer ,此技巧有效,但也有 caveats 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方式”的人来说 the DateTime class

    (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;
    
        5
  •  1
  •   Chris Baker    13 年前

    迟交的答案,但我想我会把这个答案贴出来(实际上是一个不同但相关的问题)。它处理问题中的场景:

    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

    文档

        6
  •  0
  •   Johno Vilém Kurz    12 年前

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

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

        7
  •  0
  •   carbonero    12 年前

    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');
    
        8
  •  -1
  •   Stefan Gabos    13 年前
    $monday = strtotime('Monday last week');
    $sunday = strtotime('+6 days', $monday);
    
    推荐文章