unix timestamp round to midnight

2019-01-24 00:28发布

问题:

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.

回答1:

echo date('d-m-Y H:i:s', strtotime('today', 1324189035));


回答2:

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


回答3:

Simply Use

strtotime('today midnight');