I want to find difference between two dates. For this how can I change date format and subtract two dates.
One way is to store the dates using the time() function, then you can the calculate the difference between the two dates as seconds. So if you had a difference between the two figures of 86400 that would be one day, 864000 would be ten days and so on.
I am subtracting exp_date from today's date. Exp_date is coming from table which is in the format yyyy-mm-dd. How can I change my today's date format to yyyy-mm-dd. So that I can subtract today's date from expiry date.I want difference in days. Please answer the following questions- i) what is the syntex of datediff? ii) which function I should use to find today's date? iii) How to change date format
2. Check Date() function. 3. Check explode() function, But I think you should google "php date format"... I'm sure there is something better than using explode().
function dateDiff($dformat, $endDate, $beginDate) { $date_parts1=explode($dformat, $beginDate); $date_parts2=explode($dformat, $endDate); $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]); $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]); return $end_date - $start_date; } PHP: Now let us see how we use this function: $date1="07/11/2003"; $date2="09/04/2004"; print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . "."; which generates If we minus 07/11/2003 from 09/04/2004 we get 421.