【问题标题】:time slice per day for two timestamps每天两个时间戳的时间片
【发布时间】:2013-10-31 09:55:32
【问题描述】:

我有两个时间戳,间隔在几小时到几天之间。 PHP 和/或 MySQL 中有没有办法获取每天的时间片?

INPUT
Timestamp 1: 2013-10-30 10:00:00
Timestamp 2: 2013-10-31 11:00:00

OUTPUT
2013-10-30: 14 hours
2013-10-31: 11 hours

【问题讨论】:

  • 检查 PHP 的 DateTime 对象,或者如果它们是 MySQL 时间戳,请检查 MySQL 的 DATE_ 函数以处理日期计算。但我并不完全清楚你从哪里得到 14 小时和 11 小时......
  • 时间戳 1 是开始。这一天(30 日)距离午夜还有 14 小时。结束是时间戳 2。从上午 0:00 到 11:00 是 11 个小时。

标签: php mysql timestamp


【解决方案1】:

对日期执行操作的最简单方法是将它们转换为时间戳。

这里,你可以做的是获取三个时间戳:

$ts1 = strtotime("2013-10-30 10:00:00");
$ts2 = strtotime("2013-10-31 00:00:00");
$ts3 = strtotime("2013-10-31 11:00:00");

$first_time_slice = ts2 - ts1; //here you got your first time slice in seconds, convert it to hours
$second_time_slice = ts3 - ts2; //idem for the second time slice

【讨论】:

    【解决方案2】:

    在MySql中可以这样实现:

    SELECT TIMEDIFF(TIMESTAMP(DATE('2013-10-30 10:00:00') + INTERVAL 1 DAY), '2013-10-30 10:00:00'),
    TIMEDIFF('2013-10-31 11:00:00', TIMESTAMP(DATE('2013-10-31 11:00:00')))
    

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 2011-02-02
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2016-11-20
      • 2019-08-12
      相关资源
      最近更新 更多