CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Subtracting Time from a Time - Help (http://www.codingforums.com/showthread.php?t=275472)

matt0268 10-06-2012 02:34 PM

Subtracting Time from a Time - Help
 
Code:

<?php
$channel = "mattbenson";
$json_file = @file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$channel}", 0, null, null);
$json_array = json_decode($json_file, true);

if( array_key_exists( '0',      $json_array )
 && array_key_exists( 'channel', $json_array[0] )
 && $json_array[0]['name'] == "live_user_{$channel}" )
{
    $channelTitle = $json_array[0]['channel']['title'];
    $title        = $json_array[0]['channel']['status'];
    $viewers      = $json_array[0]['channel_count'];
    $uptime      = $json_array[0]['up_time'];
$today = date("D M j d:i:s Y"); 
$uptime1 = ($today - $uptime);

    printf('Online');

printf ($uptime1);

print "<br>";
print "date <--- ";
printf ($today);
print "<br>";
print "uptime <--- ";
printf ($uptime);
}
else
{
    printf('Offline');
}

?>

It displays like this
http://gyazo.com/fb3c95029c79da2a1a0...png?1349530088

Problem is it doesn't subtract is just displays 0

Anyhelp?

Fou-Lu 10-06-2012 04:34 PM

You cannot subtract strings.
$today should be time(), and $uptime MUST be an integer. Then you can subtract and create $uptime1. That will give you the number of seconds $uptime is which you can format into whatever display.
If you have 5.3+, you can use the dateinterval.
PHP Code:

$dtUptime = new DateTime($json_array[0]['up_time']); // this now assumes a string GNU valid format
$dtNow = new DateTime();
$diDiff $dtNow->diff($dtUptimetrue);
print 
$diDiff->format("Uptime: %a days"); 



All times are GMT +1. The time now is 08:56 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.