2

Given a timestamp, what would be the most elegant solution to round that date up to the nearest midnight of the next day?

For example 1374246685 (19/07/13 10:11:25) would be rounded to 1374296400 (20/07/13 00:00:00).

James Dawson
  • 5,309
  • 20
  • 72
  • 126
  • Duplicate of http://stackoverflow.com/questions/2520404/how-do-i-find-the-unix-timestamp-for-the-start-of-the-next-day-in-php. Answer there is: $tomorrowMidnight = mktime(0, 0, 0, date('n'), date('j') + 1); – Pete Scott Jul 19 '13 at 14:16

2 Answers2

7

DateTime can do this nicely:-

$midnight = (new \DateTime())->setTimestamp(1374246685)->modify('tomorrow');

See it working

PHP version >= 5.4 only though.

Otherwise it would be:-

$midnight = new \DateTime();
$midnight->setTimestamp(1374246685)->modify('tomorrow')->setTime(0, 0);

See it working

vascowhite
  • 18,120
  • 9
  • 61
  • 77
1

Even more elegant than Pete's solution:

$tomorrowMidnight = strtotime('tomorrow');

Check out http://www.php.net/manual/en/datetime.formats.relative.php - you can do lots of fancy stuff with that :)

  • While that is pretty elegant it's not what I was after, that will give me tomorrow's midnight rather than the midnight of the next day of the passed in timestamp. :) – James Dawson Jul 19 '13 at 15:05
  • Nevermind, forgot you can pass in a timestamp in the second parameter. Thanks! – James Dawson Jul 19 '13 at 15:08