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 6 of 6

Thread: math with time

  1. #1
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post

    math with time

    whats the best way to handle figuring out how long ago it was from this date in the array storage

    PHP Code:
    [DATE] => 22 Jan 200818:23 
    from the current time?
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    $time strtotime('22 Jan 2008, 18:23');
    $time time() - $time;

    echo 
    $time ' contains the number of seconds since 22 Jan 2008, 18:23'

  • #3
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    now to get it in mins hours seconds days since last

    I would do the following:
    PHP Code:
    $str_time $time;
    $m_time $str_time 60// mins how get the reminder to show?
    $h_time $n_time /60// hours
    $d_time $h_time /24// days

    echo "last entry was: ".$d_time.days ".$h_time."hours ".$m_time."minutes ".$s_time."seconds ago"; 
    is this even close?
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    $time strtotime('22 Jan 2008, 18:23');
    $time time() - $time;
    $d_time intval($time / (24 60 60));
    $time -= $d_time * (24 60 60);
    $h_time intval($time / (60 60));
    $time -= $h_time * (60 60);
    $m_time intval($time 60);
    $time -= $m_time 60;
    $s_time $time;

    echo 
    "last entry was: $d_time days, $h_time hours, $m_time minutes, $s_time seconds ago"// last entry was: 0 days, 17 hours, 41 minutes, 28 seconds ago 

  • #5
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    how do I handle it doing this

    last entry was: -13901 days, -22 hours, -54 minutes, 0 seconds ago

    its date stamp is 23 Jan 2008, 17:54
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you want to account for dates in the future you have to adjust your code:
    PHP Code:
    $strtime strtotime('23 Jan 2008, 17:54');
    if(
    time() > $strtime)
        
    $time time() - $strtime;
    else
        
    $time $strtime time();

    $d_time intval($time / (24 60 60));
    $time -= $d_time * (24 60 60);
    $h_time intval($time / (60 60));
    $time -= $h_time * (60 60);
    $m_time intval($time 60);
    $time -= $m_time 60;
    $s_time $time;

    if(
    time() > $strtime)
        echo 
    "last entry was: $d_time days, $h_time hours, $m_time minutes, $s_time seconds ago";
    else
        echo 
    "last entry is in $d_time days, $h_time hours, $m_time minutes, $s_time seconds"
    You can use one if/else instead of two like this; this is just an example.


  •  

    Posting Permissions

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