Jump to content


Photo

php/mysql date question


  • Please log in to reply
3 replies to this topic

#1 paultfh

paultfh
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 July 2006 - 11:08 PM

How do I take a datetime type mysql and calculate how many days old it is?

Example:

$res = mysql_query("SELECT date FROM groups WHERE ID = {$arr['groupID']}");
$date = mysql_result($res2,0,0);

//here I want to calculate how many days old $date is

Thanks


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 July 2006 - 01:32 AM

Days old? You mean how many days have elapsed between NOW() and the date?  Try using the TO_DAYS() function.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 paultfh

paultfh
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 26 July 2006 - 01:40 AM

I figured it out.

My code:

                //age calculation
                $date1 = strtotime($date);
                $datenow = time();
                $diff = $datenow - $date1;
                $diff /= 86400;
             
                //if age is less than 1 day output in hours else output in days
                if($diff < 1)
                {
                        $diff *= 24;
                        $age = round($diff,1);
                        echo "<td width=\"85%\">".$age." hour(s)";
                }else{
                        $age = round($diff,1);
                        echo "<td width=\"85%\">".$age." day(s)";
             
                }



#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 July 2006 - 01:41 AM

That's one way... of course, the DB can do this for you as well without call so many PHP functions, but it's up to you.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users