简体   繁体   中英

Unix time to timestamp next day

I have the time at which the person received the assignment (1555133436), I need to display the time after which he will be able to get them again (24 hours)

I tried to do this:

$date='1555133436';

$diff=date('H:i:s', 86400- (time() - $date));
echo $diff;

In response, I get some 15:24:41

Update

It seems from your comment that you want to find out how long a user must wait to be able to see the assignment again (24 hours after they first saw it). You can achieve that with this code, which creates a DateInterval object which represents that difference and then outputs the hours and minutes remaining:

date_default_timezone_set('UTC');
$date='1555133436';
$next_date = new DateTime("@$date");
// find out when they can next access the data
$next_date->modify('+1 day');
// now find out how long they must wait
$wait = $next_date->diff(new DateTime(), true);
echo $wait->format("You must wait %h hours and %i minutes to access the data again\n");

Output:

You must wait 21 hours and 38 minutes to access the data again

Demo on 3v4l.org

Original answer (looking for the time at which the data could be accessed again)

You can convert the timestamp into a DateTime object and then add 1 day to the value:

$date='1555133436';
$next_date = (new DateTime("@$date"))->modify('+1 day');
echo $next_date->format('Y-m-d H:i:s');

Output:

2019-04-14 05:30:36

If you want the result as a timestamp too, just use

echo $next_date->format('U');

Output:

1555219836

Note that you also get that result by simply adding 86400 to the original timestamp although you should note that this method won't work on days when daylight savings starts or ends.

echo $date + 86400;

Demo on 3v4l.org

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