简体   繁体   中英

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

I need to get the days left before the date is reached, and if the timestamp is before the current date like 1990 or something, then it will display a message.

so say it's the 1st november of 2014 if the timestamp is before the first of november, 1st 2014, then it will display expired, else it will tell you how many days left before the date is reached.

thanks you very much. This is in php by the way.

What i would do is set a DateTime Class for each unix timestamp you have. Compare the two Objects with DateTime's Diff Function .

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

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

Their are multiple ways you can set the time in the DateTime Object, using timestamps or keywords or specific date formats.

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

}

The above will output

You have less than 39 days!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM