Hi,
I have recently started using PHP and I have stumbled on a problem with dates. I am writing a script, which involves a variable for a difference between two dates represented as a number of days. Simply using the date('01-04-2007') - date('01-12-2007') calculates the difference between the days but not the months and years.
Would someone please help me to write a function to work out the difference in days please? Thank you.
difference between two dates in days
Moderator: General Moderators
Feyd,
Thanks for the advise. I am sorry for being dense but I don't see how this would help me work out the difference in days. Have I got the right post? viewtopic.php?t=29341&highlight=
Thanks for the advise. I am sorry for being dense but I don't see how this would help me work out the difference in days. Have I got the right post? viewtopic.php?t=29341&highlight=
Work out the number of seconds between the two dates you have. Then pass that to the function in printf's post. It will then return an array, of which, one of the values is the number of daysTaras wrote:Feyd,
Thanks for the advise. I am sorry for being dense but I don't see how this would help me work out the difference in days. Have I got the right post? viewtopic.php?t=29341&highlight=
Thanks guys, I think I found an easier way to do this:
I found it here http://www.developertutorials.com/tutor ... page1.html
I have changed the order of date parts in the array to represent dates in the format dd/mm/yyyy
Code: Select all
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
return $end_date - $start_date;
}
print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";I have changed the order of date parts in the array to represent dates in the format dd/mm/yyyy