getting days between 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
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

getting days between dates

Post by nite4000 »

Hey all I have a simple error I am trying to fix.

I am working on a site that has listings and I need to display how long each one has been on the site in teh table i have a date_added field

This is my code

Code: Select all

 <?php $q = mysql_query("SELECT * FROM table") or die(mysql_error());
  $info = mysql_fetch_array($q, MYSQL_ASSOC);
  @mysql_free_result($q);


$date1=date('Y-m-d');
$date2=$info['date_added'];;
echo (round($diff=strtotime($date1)-strtotime($date2)))/86400;

?>

I am not sure how to get it to work.

Any help would be nice.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: getting days between dates

Post by pickle »

It looks like your `info` field is in the MySQL Date format. If so, you can easily use the MySQL DATEDIFF function function to do the calculation right in MySQL.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: getting days between dates

Post by nite4000 »

I am not sure I follow what you mean.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: getting days between dates

Post by pickle »

Check the documentation on the link I sent. It shows how you can use the DATEDIFF function. One of your parameters to the DATEDIFF function in your query can be your `info` column. The other parameter can be a string you provide that is your second, manually created date.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: getting days between dates

Post by nite4000 »

ok well i tried but maybe i am not thinking on this right

here is what i have

Code: Select all

    $q = mysql_query("SELECT DATEDIFF (date1, date_added) FROM listings") or die(mysql_error());
  $info = mysql_fetch_array($q, MYSQL_ASSOC);
  @mysql_free_result($q);


$date1=date('Y-m-d'); //todays date
$date2=$info['date_added']; //date in field for record
echo (round($diff=strtotime($date1)-strtotime($date2)))/86400;
When i echo this it needs to give the days between each date in database and current date.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: getting days between dates

Post by pickle »

If you're using your $date1 variable you need to define it before you use it. You also need to reference it in your query:

Code: Select all

$date1=date('Y-m-d'); //todays date
$q = mysql_query("SELECT DATEDIFF ('$date1', date_added) FROM listings") or die(mysql_error());
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply