Hi all,

I have two dates:

1 = $last_check = YYYY-MM-DD HH:MM:SS (A SQL value)
2 = $datetime = YYYY-MM-DD HH:MM:SS (Called using PHP)

How can I take away 1 from 2 and convert the minutes and seconds left over into seconds?

Basically I'm running an uptime counter script that will cycle through about 1000 servers. What I'm going to do is get the number of seconds since it was last checked, and if its online add them to the uptime counter in SQL and if its offline add them to the downtime counter in SQL.

It will be never be more than a few minute since the last check.

Should I use something like explode or is there a cleaner way of doing this? :

     $last_check_query = mysql_query("SELECT last_check from servers_updowntime where serverid = ".$serv_id."");
     $last_check = mysql_fetch_array($last_check_query);
     $time_since_last_check = (date("Y-m-d H:i:s") - $last_check[0]);
     $hms = ($time_since_last_check - date("Y-m-d H:i:s"));

            function time2seconds($time='00:00:00')
            {
                list($hours, $mins, $secs) = explode(':', $time);
                return ($hours * 3600 ) + ($mins * 60 ) + $secs;
            }

Use strtotime on both and subtract them.

Member Avatar for diafol

LIke P said, alternatively - how about date_diff() and use TimeInterval of seconds?

Thanks gents, I'll try them both :)

Just to check that I'm following you right...

This:

$last_check_query = mysql_query("SELECT last_check from servers_updowntime where serverid = ".$serv_id."");
     $last_check = mysql_fetch_array($last_check_query);
     $time_since_last_check = (date("Y-m-d H:i:s") - $last_check[0]);
     $hms = ($time_since_last_check - date("Y-m-d H:i:s"));
            function time2seconds($time='00:00:00')
            {
                list($hours, $mins, $secs) = explode(':', $time);
                return ($hours * 3600 ) + ($mins * 60 ) + $secs;
            }

Would become this:

$last_check_query = mysql_query("SELECT last_check from servers_updowntime where serverid = ".$serv_id."");
     $last_check = mysql_fetch_array($last_check_query);
     $time_since_last_check = (strtotime(date("Y-m-d H:i:s")) - $last_check[0]);
     $hms = ($time_since_last_check - strtotime(date("Y-m-d H:i:s")));
            function time2seconds($time='00:00:00')
            {
                list($hours, $mins, $secs) = explode(':', $time);
                return ($hours * 3600 ) + ($mins * 60 ) + $secs;
            }

?

Found this beauty on php.net user submissions:

function date_diff($start, $end="NOW")
{
        $sdate = strtotime($start);
        $edate = strtotime($end);

        $time = $edate - $sdate;
        if($time>=0 && $time<=59) {
                // Seconds
                $timeshift = $time.' seconds ';

        } elseif($time>=60 && $time<=3599) {
                // Minutes + Seconds
                $pmin = ($edate - $sdate) / 60;
                $premin = explode('.', $pmin);

                $presec = $pmin-$premin[0];
                $sec = $presec*60;

                $timeshift = $premin[0].' min '.round($sec,0).' sec ';

        } elseif($time>=3600 && $time<=86399) {
                // Hours + Minutes
                $phour = ($edate - $sdate) / 3600;
                $prehour = explode('.',$phour);

                $premin = $phour-$prehour[0];
                $min = explode('.',$premin*60);

                $presec = '0.'.$min[1];
                $sec = $presec*60;

                $timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec ';

        } elseif($time>=86400) {
                // Days + Hours + Minutes
                $pday = ($edate - $sdate) / 86400;
                $preday = explode('.',$pday);

                $phour = $pday-$preday[0];
                $prehour = explode('.',$phour*24); 

                $premin = ($phour*24)-$prehour[0];
                $min = explode('.',$premin*60);

                $presec = '0.'.$min[1];
                $sec = $presec*60;

                $timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec ';

        }
        return $timeshift;
}

// EXAMPLE:

$start_date = 2010-03-15 13:00:00
$end_date = 2010-03-17 09:36:15

echo date_diff($start_date, $end_date);

?>

"
Returns: 1days 20hours 36min 15sec
Can be taken up to centuries - if you do the calculations.

Hope this finally helps someone! :D
"
Will test and if it works mark as resolved :)

Member Avatar for diafol

Be aware, that's not the php date_diff() function, but an user-defined function. Not the one I had in mind, but if it works, why not.

commented: cheers :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.