Jump to content

Archived

This topic is now archived and is closed to further replies.

paultfh

php/mysql date question

Recommended Posts

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

Share this post


Link to post
Share on other sites
Days old? You mean how many days have elapsed between NOW() and the date?  Try using the TO_DAYS() function.

Share this post


Link to post
Share on other sites
I figured it out.

My code:
[quote]                //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)";
             
                } [/quote]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.