【问题标题】:Get strtotime to find next timestamp at given time获取 strtotime 以在给定时间查找下一个时间戳
【发布时间】:2013-11-26 20:48:39
【问题描述】:

我正在尝试让 strtotime 给我一个时间戳,但我只知道它是一个小时和分钟,并且它是该行中的下一个。外汇。

如果给定的小时和分钟是 01:00 (AM),我要求它返回 01:00 (AM) 的时间戳,如果是前一天的 19:00,我希望它返回时间戳次日 01:00,但如果是在午夜 00:30 之后,则应返回同一日期 01:00 的时间戳。

为了简化,它是一个时间的下一次出现。

Strtotime 给我的是同一天的时间,如果不是下一次。

【问题讨论】:

  • 您可以发布您尝试过的代码的 sn-p 吗? PHP 有几种处理日期/时间的方法,因此了解您选择的方向会很有帮助。
  • $start = strtotime("next ".$_POST['starttime']);

标签: php timestamp strtotime


【解决方案1】:

您可以轻松生成时间戳,查看它是否已经过去,然后增加日期。

大概是这样的吧?

$timestamp = strtotime("1:00 am");
if($timestamp < time()) {
    $timestamp = strtotime("+1 day",$timestamp);
}

或者,如果您想将其收紧为单线:

$string = "1:00 am";
$timestamp = (strtotime($string) > time()) ? strtotime($string) : strtotime("tomorrow " . $string);

【讨论】:

  • 但是如果我们过了午夜呢?然后它返回正确的时间戳而不添加 1 天。
  • 猜我不明白这个问题。如果是星期二下午 4 点,此解决方案将为您提供星期三凌晨 1 点。如果是星期三凌晨 12:30,此解决方案仍会为您提供星期三凌晨 1 点。这不是你想要的吗?
  • @RoboForm 举个例子,这个解决方案无法提供你想要的,我不确定我看到了。
  • @RoboForm 很高兴为您提供帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2021-05-05
  • 1970-01-01
  • 2019-09-02
相关资源
最近更新 更多