Dates

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
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Dates

Post by icesolid »

I wanted to know if anyone had any ideas on comparing two dates.

I have two dates which are dynamically pulled from a database (stored as DATE format), below are just two example dates:

Code: Select all

<?php
$date1 = "2006-08-11";
$date2 = "2006-08-20";
?>
I want to figure out how to have PHP compare the two and give me the number of days in-between the dates and store it in a variable.

Code: Select all

<?php
$total_days = answer here;
?>
Last edited by icesolid on Fri Sep 08, 2006 12:23 pm, edited 3 times in total.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

it's going to be much easier to handle this on the database end. Take a look at the mysql's datediff(exp1, exp2) function
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I tried the using your suggestion this is what I have and I am not producing a correct result.

Code: Select all

$time_service = mysql_query("SELECT DATEDIFF('2006-08-11','2006-08-20') FROM cases WHERE control_number='$control_number'");
When I echo out $time_service this is my result:

Code: Select all

Resource id #8
When I try and put this result in an array (mysql_fetch_array) I get Array as my result.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=29341

After as many posts as you have icesolid, should I need to remind you to use

Code: Select all

tags?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

That's because mysql_query does not return a string or an array but a resource.
See http://de2.php.net/myql_fetch_array
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Sorry

Post by icesolid »

I'm sorry feyd, honestly.

I have figured out my problem, well both of them, I am an idiot and my programmatic error.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Also don't forget the 'AS' part

Code: Select all

SELECT DATEDIFF('2006-08-11','2006-08-20') AS alias_name
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Hey....

Look what I've found for you

http://www.ilovejackdaniels.com/accessi ... placement/
Post Reply