戸惑うstrtotime

4542 ワード

      strtotime  -1 month, +1 month, next month        ,                  ,        .         …
  ,             :
   2018-07-31     :
date("Y-m-d",strtotime("-1 month"))
     2018-07-01?
   ,             ,           ,    ” ” ,       ,      :
      date              :
  • はまず-1 monthをして、それでは今07-31で、1を減らしてから06-31です.6月は31日がないので、
  • 日を規範化します.
  • だから2時60が3時に等しいように、6月31は7月1
  • に等しい.
    論理が「はっきりしている」のではないでしょうか.2つ目のステップを手動で検証することもできます.たとえば、次のようにします.
    var_dump(date("Y-m-d", strtotime("2017-06-31")));
    //  2017-07-01
        ,              ,          ,                   ,       :
    
    var_dump(date("Y-m-d", strtotime("-1 month", strtotime("2017-03-31"))));
    //  2017-03-03
    var_dump(date("Y-m-d", strtotime("+1 month", strtotime("2017-08-31"))));
    //  2017-10-01
    var_dump(date("Y-m-d", strtotime("next month", strtotime("2017-01-31"))));
    //  2017-03-03
    var_dump(date("Y-m-d", strtotime("last month", strtotime("2017-03-31"))));
    //  2017-03-03
         ?
    
     PHP5.3   , date          ,        ,    ”first day of”   “last day of”,             date  ”   ”:
    
    var_dump(date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31"))));
    //  2017-02-28
    var_dump(date("Y-m-d", strtotime("first day of +1 month", strtotime("2017-08-31"))));
    ////  2017-09-01
    var_dump(date("Y-m-d", strtotime("first day of next month", strtotime("2017-01-31"))));
    ////  2017-02-01
    var_dump(date("Y-m-d", strtotime("last day of last month", strtotime("2017-03-31"))));
    ////  2017-02-28
        5.3     (     ?),      mktime   ,          ,         1     ,          first day      .
    
      ,         ,        ?  ��