【问题标题】:Getting midnight timestamp value from specific timestamp从特定时间戳获取午夜时间戳值
【发布时间】:2016-04-19 05:46:10
【问题描述】:

我只想计算时间戳的午夜值。例如 1461043742 这是今天的时间戳,从中我想要午夜 12 点时钟时间戳值的时间戳。这是 1461130928 明天的时间戳,我想显示明天午夜 12 点的时间戳值。我该怎么做,有没有可能在 PHP 中实现这一点?

【问题讨论】:

    标签: php unix-timestamp


    【解决方案1】:

    你可以使用strtotime:

    // Input:
    $now = 1461043742;
    
    // Calculate:
    $midnight = strtotime("midnight", $now);
    $midnight_tomorrow = strtotime("midnight tomorrow", $now);
    
    // Test
    print(date('c', $midnight));
    print(date('c', $midnight_tomorrow));
    

    【讨论】:

      【解决方案2】:

      您需要将小时和分钟更改为午夜。

      $date = new DateTime() // By default gets the current time
      $date.setTime(0,0,0) // Midnight hour
      $stamp = date.getTimestamp()
      

      【讨论】:

        【解决方案3】:

        你可以使用 strtotime

        $date = '2016-04-18 00:00:00';
        $timestamp = strtotime($date);
        
        $date = date('Y-m-d 00:00:00');
        $timestamp = strtotime($date);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-19
          • 1970-01-01
          • 2012-01-22
          • 1970-01-01
          • 1970-01-01
          • 2013-04-27
          • 2022-01-16
          相关资源
          最近更新 更多