by prettyscripts on 2010-02-19 11:19:00
there are a few ways to calculate how many days between 2 given dates.
the following examples calculates how many days to x'mas from today (19/2/10).
usingĀ gregoriantojd($month, $day, $year) function to find out the julian day count. without going into the detail what are julian calendar and gregorian calendar, we just wanted the day count to calculate the difference.
PHP:
gregoriantojd(12, 25, 2010) - gregoriantojd(2, 19, 2010); |
using strtotime() function to find out the unix timestamp in seconds of the date and calcuate the difference in seconds and convert it to days.
PHP:
note date format passed to strtotime() function. since there are different formats used throughout the world, use 'yyyy-mm-dd' just to be safe.
personally i like method 1. i don't have to worry about the date format since it takes date parts as parameter and the function returns in days.