How do I find the unix timestamp for the start of

2019-02-16 06:21发布

I have a unix timestamp for the current time. I want to get the unix timestamp for the start of the next day.

$current_timestamp = time();
$allowable_start_date = strtotime('+1 day', $current_timestamp);

As I am doing it now, I am simply adding 1 whole entire day to the unix timestamp, when instead I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.

What is the best way to go about this?

6条回答
The star\"
2楼-- · 2019-02-16 06:24

You can easily get tomorrow at midnight timestamp with:

$tomorrow_timestamp = strtotime('tomorrow');

If you want to be able to do a variable amount of days you could easily do it like so:

$days = 4;
$x_num_days_timestamp = strtotime(date('m/d/Y', strtotime("+$days days"))));
查看更多
孤傲高冷的网名
3楼-- · 2019-02-16 06:29

The most straightforward way to simply "make" that time:

$tomorrowMidnight = mktime(0, 0, 0, date('n'), date('j') + 1);

Quote:

I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.

Don't do it like that. Avoid relative calculations whenever possible, especially if it's so trivial to "absolutely" get the timestamp without seconds arithmetics.

查看更多
Rolldiameter
4楼-- · 2019-02-16 06:38

My variant:

 $allowable_start_date = strtotime('today +1 day');
查看更多
成全新的幸福
5楼-- · 2019-02-16 06:40
$tomorrow = strtotime('+1 day', strtotime(date('Y-m-d')));
$secondsLeftToday = time() - $tomorrow;
查看更多
女痞
6楼-- · 2019-02-16 06:45

Something simple like:

$nextday = $current_timestamp + 86400 - ($current_timestamp % 86400);

is what I'd use.

查看更多
一夜七次
7楼-- · 2019-02-16 06:46

The start of the next day is calculated like this:

<?php

$current_timestamp = time();
$allowable_start_date = strtotime('tomorrow', $current_timestamp);

echo date('r', $allowable_start_date);

?>

If it needs to follow your peculiar requirement:

<?php

$current_timestamp = time();
$seconds_to_add = strtotime('tomorrow', $current_timestamp) - $current_timestamp;

echo date('r', $current_timestamp + $seconds_to_add);

?>
查看更多
登录 后发表回答