27

If I have a random unix timestamp, how can I round it down to today's midnight or the midnight selected by the user. The reason for this is that I want to add hours and minutes after a certain day's midnight.

For example if the timestamp is 1324189035 then how can I remove the hours, minutes, and seconds to put the timestamp at midnight for that day.

user962449
  • 3,743
  • 9
  • 38
  • 53

6 Answers6

24
echo date('d-m-Y H:i:s', strtotime('today', 1324189035));
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 2
    this is beautiful. I had see one where someone thinking outside of the box put like `$roundedStamp = $time - ($time % 86400);` and it was clever but not perfect. It rounds up or down depending so it doesn't work. This does :) – Yes Barry Aug 27 '13 at 00:12
  • This will return a date string, you need to add `strtotime` in order to get timestamp of midnight that should wrap around the entire code that is being echo'ed. – Solomon Closson Jan 07 '18 at 04:46
  • @SolomonClosson `strtotime('today', 1324189035)` already there and does the thing, `date` is just to demonstrate it is actually the midnight. – zerkms Jan 07 '18 at 08:23
  • awww, gotcha, got a bit confused there with the `date` echo. – Solomon Closson Jan 07 '18 at 19:37
  • In the desperate hope that this might help someone in my situation, a slight adapatation of this to get **number** of days from UNIX epoch (from the same timestamp to eliminate rounding errors): `round(strtotime('today', time()) / 86400))`. My use case was finding odd numbered days since Epoch to alternate a cron procedure `round($daysFromUnixEpoch) % 2 === 0` – Scott Anderson Apr 29 '21 at 13:42
18

Because of how you're using it, I wouldn't calculate midnight at all: it is far easier to simply convert what you're adding to the timestamp into 24 hour time and then use strtotime:

echo strtotime("0:00",1324189035); // 1324184400
echo strtotime("17:50",1324189035); // 1324248600

And if you want to have that in human readable, use date and m/d/Y H:i:s:

echo date('m/d/Y H:i:s', strtotime('17:50',1324189035)); // 12/18/2011 17:50:00
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • This solution is most elegant and pleasing. It needs to be remembered that this is all GMT, so to get 17:50 in any other timezone, the time offset would need to be added before this time mapping, then taken off again afterwards (assuming you are ultimately looking for a GMT/UTC time for searching data in a database, for example). – Jason Apr 12 '12 at 16:35
  • It seems to map midnight to the *previous* midnight: `echo strtotime("0:00 +0000", 1443441600);` prints `1443398400` – Rag Oct 01 '15 at 02:02
8

Simply Use

strtotime('today midnight');
M_R_K
  • 5,929
  • 1
  • 39
  • 40
1

Just do

date('d-m-Y',strtotime('today'));

Easy!

1

An easy solution would be to use the modulo expression to remove the exceeded seconds from a round day timestamp.

<?php

date_default_timezone_set('UTC');

$timestamp = time();

echo "timestamp : " . $timestamp . PHP_EOL;
echo "timestamp formatted : " . date('Y-m-d H:i:s', $timestamp) . PHP_EOL;

$diff = $timestamp % (60 * 60 * 24);

echo "diff : " . $diff . PHP_EOL;

$midnight = $timestamp - $diff;

echo "midnight : " . $midnight . PHP_EOL;
echo "midnight formatted : " . date('Y-m-d H:i:s', $midnight) . PHP_EOL;

This would output the following result.

timestamp : 1575451074

timestamp formatted : 2019-12-04 09:17:54

diff midnight : 1575417600

midnight formatted : 2019-12-04 00:00:00

And here is a one liner function to get your midnight from any timestamp.

function getMidnight ($timestamp) { return $timestamp - ($timestamp % 86400); }

http://sandbox.onlinephpfunctions.com/code/1d85c935e71fcf011284ae33658e0c68dd8d8c28

Community
  • 1
  • 1
Tim
  • 1,238
  • 1
  • 14
  • 24
0

How about just: date -d $(date +%F) +%s

Joe the Person
  • 4,470
  • 3
  • 22
  • 32