Page 1 of 1
difference between two dates in days
Posted: Thu Sep 20, 2007 9:40 am
by Taras
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.
Posted: Thu Sep 20, 2007 10:14 am
by feyd
Look in Code Snippets for a thread created by printf (that's the username, not the function.)
Posted: Fri Sep 21, 2007 3:44 am
by Taras
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=
Posted: Fri Sep 21, 2007 4:50 am
by JayBird
Taras 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=
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 days
Posted: Fri Sep 21, 2007 5:11 am
by Taras
Thanks guys, I think I found an easier way to do this:
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 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