Page 1 of 4 123 ... LastLast
Results 1 to 10 of 35

Thread: time converter

  1. #1
    Join Date
    Aug 2009
    Posts
    398
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default time converter

    Hi, maybe someone can help me with this...
    I have
    include('db_conn.php');
    $queryget = mysql_query("SELECT * FROM reg_users ORDER BY `time` LIMIT 10") or die("Error with query");
    while ($row = mysql_fetch_array($queryget))
    {
    $date = $row['date'];
    $time = $row['time'];
    $datet = date("Y-m-d");
    $timet = date("H:i:s");

    time's are saved as you can see: H:i:s (23:10:55)
    so, i need to convert $time = $row['time']; and $timet = date("H:i:s"); to seconds... is there an option? THANKS
    Last edited by auriaks; 11-14-2009 at 10:14 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You can do this two ways (I suggest the second)

    #1
    (To get seconds
    PHP Code:
    $seconds explode(':'$timet)[2]; 
    #2 (highly suggested)
    Instead of saving times as H:i:s, I would save times($row['time']) using the time() function. Then when getting the time as H:i:s you would do:
    PHP Code:
    $time date("H:i:s"$row['time']);
    echo 
    $time// 23:10:55
    $seconds date("s"$row['time']);
    echo 
    $seconds//55 
    Feel free to ask questions.
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2009
    Posts
    398
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    what does it means [2] ?? because of that i got an error and i cant open my page

  4. #4
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Sorry. :/
    PHP Code:
    $seconds explode(':'$timet);
    $seconds $seconds[2]; 
    Jeremy | jfein.net

  5. #5
    Join Date
    Aug 2009
    Posts
    398
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    it works, but takes only seconds. I need all time convert to seconds like 2H 5m 32s = 7532 seconds

  6. #6
    Join Date
    Aug 2009
    Posts
    398
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    what i have to write?

  7. #7
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Haha, sorry:
    Code:
    $total_seconds = 0;
    $seconds = explode(':', $timet);
    $total_seconds += $seconds[0] * (60 * 60);
    $total_seconds += $seconds[1] * 60;
    $total_seconds += $seconds[2];
    echo $total_seconds;
    It is not tested!
    Jeremy | jfein.net

  8. #8
    Join Date
    Aug 2009
    Posts
    398
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Yea, it's working
    Last edited by auriaks; 11-26-2009 at 09:09 PM.

  9. #9
    Join Date
    Aug 2009
    Posts
    398
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Hi,
    I faced problem of this function, but don't know where
    I use this: [also I update their mysql time in every move]
    PHP Code:
    ...

    function 
    timeDiff($firstTime,$lastTime) {
     
    $firstTime=strtotime($firstTime);
     
    $lastTime=strtotime($lastTime);
     
    $timeDiff=$lastTime-$firstTime;
     return 
    $timeDiff;
     echo 
    "$timeDiff";
    }  

    while (
    $row mysql_fetch_array($queryget)) 

        
    $nick $row['nick'];    
        
    $time $row['time'];
        
    $timet date("H:i:s");
        
    $id $row['id'];
        
    if (
    timeDiff($time,$timet) <= 300) {
    if (
    timeDiff($time,$timet) >= -1) {
        
    $var '1';
        echo 
    "
        
    $nick user is online
        "

    }}} 
    if (
    $var 1) {echo "noone is online";} 
    and no users are online ALWAYS.
    Thanks

  10. #10
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    First of all
    PHP Code:
     return $timeDiff;
     echo 
    "$timeDiff"
    Onc you return something, the whole function is stopped.

    Second of all
    PHP Code:
    if (timeDiff($time,$timet) <= 300) {
    if (
    timeDiff($time,$timet) >= -1) { 
    Whats the point of that, just do:
    PHP Code:
    if ((timeDiff($time,$timet) <= 300) AND timeDiff($time,$timet) >= -1) { 
    Next,
    PHP Code:
    $var '1'
    Even though PHP works with strings as numbers, you may want to do:
    PHP Code:
    $var 1
    Instead.

    And finally - what problem are you facing?
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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