如何确定距离unix时间戳日期还有多少天


How to determine how many days left before a unix timestamp date?

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

假设是2014年11月1日如果时间戳在2014年11月1日之前,那么它将显示过期,否则它将告诉您距离到达日期还有多少天。

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

<?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!

我要做的是为您拥有的每个unix时间戳设置一个DateTime类。将这两个对象与DateTime的Diff函数进行比较。

$TodaysDate = new DateTime();
$TodaysDate->setTimestamp(time());
$ExperationDate= new DateTime('2014-10-01');
$interval = $TodaysDate->diff($ExperationDate);
If($interval <= 0){
   echo 'Product Expired';
}

它们有多种方法,可以使用时间戳或关键字或特定的日期格式在DateTime对象中设置时间。

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