【问题标题】:How to determine how many days left before a unix timestamp date?如何确定距离 unix 时间戳日期还有多少天?
【发布时间】:2014-09-23 02:15:16
【问题描述】:

我需要获取到达日期之前的剩余天数,如果时间戳在当前日期之前,例如 1990 年或其他日期,那么它将显示一条消息。

所以说这是 2014 年 11 月 1 日 如果时间戳在 2014 年 11 月 1 日之前,那么它将显示已过期,否则它将告诉您距离日期还剩多少天。

非常感谢。 顺便说一句,这是在php中。

【问题讨论】:

  • 到目前为止你有什么想法?时间是怎么过去的?没有看到这个,我们不能给你相关的答案。

标签: php date unix time


【解决方案1】:
<?php

$current = time();
$target = '2014-11-01 00:00:00';
$target = strtotime($target);

if($target > $current){

     $span = $target - $current;
     $span = ceil($span / (60 * 60 * 24));

     echo 'You have less than '.$span.' days!';

} else {

    echo 'Time has already expired!';

}

上面会输出

You have less than 39 days!

【讨论】:

  • 地板应该代替天花板
【解决方案2】:

我要做的是为您拥有的每个 unix 时间戳设置一个 DateTime Class。将这两个 Object 与 DateTime 的 Diff Function 进行比较。

$TodaysDate = new DateTime();
$TodaysDate->setTimestamp(time());
$ExperationDate= new DateTime('2014-10-01');
$interval = $TodaysDate->diff($ExperationDate);

If($interval <= 0){
   echo 'Product Expired';
}

您可以通过多种方式在 DateTime 对象中设置时间,使用时间戳或关键字或特定日期格式。

【讨论】:

    【解决方案3】:

    只需从您的时间戳中减去当前时间并除以 - Unix 时间以秒为单位。

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 2021-11-20
      • 2012-08-13
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2014-05-29
      • 2012-08-02
      • 2013-07-15
      相关资源
      最近更新 更多