difference between two dates in days

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Taras
Forum Newbie
Posts: 7
Joined: Thu Sep 20, 2007 9:21 am
Location: West Yorkshire, UK

difference between two dates in days

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look in Code Snippets for a thread created by printf (that's the username, not the function.)
Taras
Forum Newbie
Posts: 7
Joined: Thu Sep 20, 2007 9:21 am
Location: West Yorkshire, UK

Post 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=
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
Taras
Forum Newbie
Posts: 7
Joined: Thu Sep 20, 2007 9:21 am
Location: West Yorkshire, UK

Post 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
Post Reply