20

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?

zeckdude
  • 15,877
  • 43
  • 139
  • 187

6 Answers6

28

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 2
    So this is the same as strtotime('+1 day', mktime(0, 0, 0))? – zeckdude Mar 26 '10 at 23:48
  • 2
    @zeckdude Essentially yes, and I would say more efficient, since `strtotime` is powerful, but slower. Also you're explicitly making a timestamp *at midnight*, not adding a day to a timestamp at midnight, which may or may not be more reliable in edge cases like daylight saving switchovers etc. – deceze Mar 27 '10 at 01:02
10

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"))));
tony4d
  • 166
  • 3
  • As deceze pointed out above strtotime('+1 day', mktime(0, 0, 0)) is more elegant than the nested solution I gave. So: $days = 4; $x_num_days_timestamp = strtotime("+$days days", mktime(0, 0, 0)); – tony4d Mar 26 '10 at 08:25
4
$tomorrow = strtotime('+1 day', strtotime(date('Y-m-d')));
$secondsLeftToday = time() - $tomorrow;
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
Amy B
  • 17,874
  • 12
  • 64
  • 83
2

Something simple like:

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

is what I'd use.

Mike Anchor
  • 666
  • 4
  • 4
  • what is the 86400? what does that number represent? – zeckdude Mar 26 '10 at 00:32
  • So If I would want to use this same idea but to get the timestamp for the start of two days from now, I would just double that number, such as $twodays = $current_timestamp + 172800 - ($current_timestamp % 172800); ? – zeckdude Mar 26 '10 at 00:39
  • 1
    86400 is the number of seconds in a day – Mike Anchor Mar 26 '10 at 00:40
  • This is wrong, because the day does not always have 24 hours. See [daylight saving time](https://en.wikipedia.org/wiki/Daylight_saving_time). – PiTheNumber Oct 07 '20 at 12:17
0

My variant:

 $allowable_start_date = strtotime('today +1 day');
pliashkou
  • 4,052
  • 2
  • 31
  • 39
0

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);

?>
Álvaro González
  • 142,137
  • 41
  • 261
  • 360