Page 1 of 1
Round date to nearest given date
Posted: Thu Jan 07, 2010 7:50 am
by klevis miho
I have a list of dates.
How can I round, for example the todays date, to the nearest date on the list?
Re: Round date to nearest given date
Posted: Thu Jan 07, 2010 10:46 am
by manohoo
1. put the dates in an array
2. cycle the array, find the difference (PHP can return result in seconds) between today and the date
3. the minimum difference is associated with the closest date
Can you code the above?
Of course, you can do the calculation in days, hrs... whatever you decide
Re: Round date to nearest given date
Posted: Thu Jan 07, 2010 11:18 am
by klevis miho
You are right.
But if I want to find the nearest hour date("H:i"). Let's skip the dates, just for the hours.
Re: Round date to nearest given date
Posted: Thu Jan 07, 2010 2:14 pm
by pickle
Convert them to UNIX timestamps & you can use whatever metric ( > 1 second ) you want