Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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


    Problem is it doesn't subtract is just displays 0

    Anyhelp?
    Last edited by matt0268; 10-06-2012 at 02:46 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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"); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •