【问题标题】:get unix timestamp using php使用 php 获取 unix 时间戳
【发布时间】:2011-01-17 08:47:00
【问题描述】:

假设我知道今天是星期一。 我如何在 php 中使用mktime() 来获取上周五和之前的周五的 unix 时间戳?

假设今天的日期是 17-01-2011 并且是星期一。然后我想要 14-01-2011 00:00:00 和 7-01-2011 00:00:00 的时间戳。

【问题讨论】:

标签: php timestamp


【解决方案1】:

查看strtotime http://php.net/manual/en/function.strtotime.php ..解决了大部分此类问题 - 否则您必须自己确定日期

【讨论】:

  • 所以,具体的例子是 strtotime('last friday') 然后减去 86400 * 7(86400 秒乘以 7 天)。
  • @jishi 确实,或者在给定的 php.net 链接的示例下列出的所有内容:D
  • @yishi 从时间戳中减去硬编码的秒数会在 DST 偏移时导致错误的结果。 strtotime 支持 DST,因此只需使用问题下方链接中给出的相对格式即可。
【解决方案2】:

应该这样做

<?php
    // Check if today is a Monday
    if (date('N') == 1)
    {
        // Create timestamp for today at 00:00:00
        $today = mktime('0', '0', '0', date('n'), date('j'), date('Y'));

        $last_friday = $today - 60*60*24*3;
        $last_last_friday = $today - 60*60*24*10;

        // Convert to a readable format as a check
        echo 'Last Friday\'s timestamp is ' . $last_friday . ' (' . strftime('%d-%m-%Y %H:%M:%S', $last_friday).') <br />';  
        echo 'Last last Friday\'s timestamp is ' . $last_last_friday . ' (' . strftime('%d-%m-%Y %H:%M:%S', $last_last_friday).')';  
    }

【讨论】:

    【解决方案3】:

    比您想象的要简单得多(或者甚至我)!本质上,strtotime("last friday", time()) 获得了……上周五的时间戳!

    首先获取当前时间戳:

    $unixNow = time();
    echo date('r', $unixNow) . "<br />";
    //Thu, 10 Jan 2013 15:14:19 +0000
    

    上周五去:

    $unixLastFriday = strtotime("last friday", $unixNow);
    echo date('r', $unixLastFriday) . "<br />";
    //Fri, 04 Jan 2013 00:00:00 +0000
    

    在那之前的星期五:

    $unixFridayBeforeThat = strtotime("last friday", $unixLastFriday);
    echo date('r', $unixFridayBeforeThat) . "<br />";
    //Fri, 28 Dec 2012 00:00:00 +0000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      相关资源
      最近更新 更多