Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix timestamp round to midnight

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.

like image 567
user962449 Avatar asked Dec 18 '11 06:12

user962449


People also ask

How do you round a timestamp to an hour?

Round time to nearest hour ( TIME(1,0,0) = 1/24 representing an hour) and add a zero time value to ensure the expression is cast as a Time. M: There isn't an equivalent of MROUND in M, so instead multiply the time by 24 to express it as a number of hours, then round, then divide by 24 and convert to a Time type.

What is midnight timestamp?

Using a database querying tool, a midnight timestamp is treated as the start of the date, but in Clarity, it is interpreted as the day before if this timestamp is used for a Finish Date.

Is Unix timestamp always UTC?

A few things you should know about Unix timestamps:Unix timestamps are always based on UTC (otherwise known as GMT). It is illogical to think of a Unix timestamp as being in any particular time zone. Unix timestamps do not account for leap seconds.

Is Unix time based on UTC?

Unix time is the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970, excluding leap seconds. This time is named the Unix epoch, because it is the start of the Unix time. Unix time is not a true representation of UTC, because leap seconds are not independently represented.


2 Answers

echo date('d-m-Y H:i:s', strtotime('today', 1324189035));
like image 118
zerkms Avatar answered Oct 22 '22 04:10

zerkms


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
like image 18
cwallenpoole Avatar answered Oct 22 '22 05:10

cwallenpoole