2014-09-23 248 views
0

我需要获得日期到达之前的日子,如果时间戳在1990年之前的当前日期之前,那么它将显示一条消息。如何确定unix时间戳日期之前剩余多少天?

所以说它是2014年11月1日 如果时间戳在2014年11月1日之前,那么它将显示过期,否则它会告诉你在达到日期之前还有多少天。

非常感谢你。 这是在顺便说一句。

+0

到目前为止你想出了什么?时间过得如何?没有看到这个,我们不能给你一个相关的答案。 – mschuett 2014-09-23 02:16:57

回答

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

地板应该用来代替ceil – 2017-08-12 09:25:34

0

我会做的是设置一个DateTime Class你有每个unix时间戳。将两个对象与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对象中设置时间。

0

只是减去你的时间戳和除以当前时间 - Unix时间以秒为单位。