i want to calculate difference of 2 dates and i have a little error. if the days are in the same month everything works perfect. but if 1 day is from a month ago the result is not good. for example: $now= date('d.m.Y'); $date= "28.07.2009" ; $diference= ($now - $date); echo "$diference"; it print's me something like -18.99 what is wrong?
You should convert the date to a timestamp to do any sort of mathimatical operation on it. Something like: $now = time(); $then = strtotime("28.07.2009"); $difference = $now - $then; //In Seconds You can convert seconds to whatever denomination you need. *60 for Minutes, *3600 For Hours, etc...
You can't subtract strings. You'll need to convert the string-based format to an actual unix time (strtotime() I belive would do it). Then you can subtract the earlier time from the later time to get an actual difference in value. Otherwise what you're doing is "9.08.2009' - "28.07.2009" which mathematically makes no sense.
It works. $date1 = $row['date'] ; $date2 = date('d.m.Y'); $d1 = explode(".",$date1); $d2 = explode(".",$date2); $days = floor(abs(mktime(0,0,0,$d1[1],$d1[0],$d1[2]) - mktime(0,0,0,$d2[1],$d2[0],$d2[2])) / (60*60*24)); echo $days; Thank you @jestep