代码之家  ›  专栏  ›  技术社区  ›  Mike Crittenden

在PHP中获取正好一周前的时间戳?

  •  35
  • Mike Crittenden  · 技术社区  · 16 年前

    我需要使用PHP计算7天前的时间戳,因此如果当前是3月25日晚上7:30,它将返回3月18日晚上7:30的时间戳。

    我应该从当前时间戳中减去604800秒,还是有更好的方法?

    6 回复  |  直到 16 年前
        1
  •  87
  •   SilentGhost    16 年前
    strtotime("-1 week")
    
        2
  •  26
  •   Ben Everard    10 年前

    strtotime 他是你的朋友

    echo strtotime("-1 week");
    
        3
  •  11
  •   Aaron W.    16 年前

    http://php.net/strtotime

    echo strtotime("-1 week");
    
        4
  •  9
  •   Luís Guilherme    16 年前

    PHP.net

    <?php
      $nextWeek = time() + (7 * 24 * 60 * 60);
                   // 7 days; 24 hours; 60 mins; 60secs
      echo 'Now:       '. date('Y-m-d') ."\n";
      echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
      // or using strtotime():
      echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n";
    ?>
    

    在第一行(或最后一行)将+改为-将得到您想要的。

        5
  •  4
  •   Paweł Tomkiel    11 年前

    菲律宾比索5.2 你可以用 DateTime :

    $timestring="2015-03-25";
    $datetime=new DateTime($timestring);
    $datetime->modify('-7 day');
    echo $datetime->format("Y-m-d"); //2015-03-18
    

    而不是创造 DateTime setTimestamp 直接在对象上:

    $timestamp=1427241600;//2015-03-25
    $datetime=new DateTime();
    $datetime->setTimestamp($timestamp);
    $datetime->modify('-7 day');
    echo $datetime->format("Y-m-d"); //2015-03-18
    
        6
  •  0
  •   Andrew Barber Eric Lafortune    13 年前
    <?php 
       $before_seven_day = $date_timestamp - (7 * 24 * 60 * 60)
       // $date_timestamp is the date from where you found to find out the timestamp.
    ?>
    

    还可以使用string to time函数将日期转换为时间戳。喜欢

    strtotime(23-09-2013);