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 5 of 5
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,423
    Thanks
    69
    Thanked 102 Times in 101 Posts

    Check datetime into hour minute

    Is it possible to take the datetime format of 2011-12-10 09:45:18
    and convert it into something like this: 27 Hours, 34 Minutes Remaining?

    I have a market to buy and sell items, and need to show the hours and minutes remaining until it ends, since the date time is confusing to some people.

    I store it in the database.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 11 Times in 11 Posts
    This will give you the number of seconds remaining, such as "318583".

    echo time() - strtotime('2011-12-10 09:45:18');

    You can then use the number to output the time remaining in whatever format you like. I don't know if there is a function to produce the text you want, or you can do the calculations for days, hours and minutes remaining yourself.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by >ssp-cdr< View Post
    This will give you the number of seconds remaining, such as "318583".

    echo time() - strtotime('2011-12-10 09:45:18');

    You can then use the number to output the time remaining in whatever format you like. I don't know if there is a function to produce the text you want, or you can do the calculations for days, hours and minutes remaining yourself.
    It would be the other way round - the time/date he wants is in the future. Correct in your thinking though - I need to pop out for a couple of minutes, but when I'm back I'll post a solution.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    PHP Code:
    function getRemaining($from,$until){
        if(
    $until <= $from){
            
    // Time has already elapsed
            
    return FALSE;
        }
        else{
            
    // Get time remaining
            
    $results = array();
            
    $time $until $from;
            
    $minutes floor($time/60);
            
    $hoursFloat $minutes/60;
            
    $results['hours'] = floor($hoursFloat);
            
    $results['minutes'] = ($hoursFloat-$results['hours'])*60;
            return 
    $results;
        }

    Returns an array with two indexes - hours and minutes, or returns false if the time supplied in $until (the time in the future) is less than that in $from. Suggested use:
    PHP Code:
    $array getRemaining(time(),strtotime($futureTime));
    if(!
    $array){
        echo 
    'Closed';
    else{
        echo ( (
    $array['hours'] != 0) ? $array['hours'].' hour'.(($array['hours']>1) ? 's':'').(($array['minutes'] != 0) ? ' and '.$array['minutes'].' minute'.(($array['minutes']>1) ? 's''').' remaining' ' remaining') : (($array['minutes'] != 0) ? $array['minutes'].' minute'.(($array['minutes']>1) ? 's''').' remaining' ''));

    That echo might look a bit messy, but it's just a mixture of ternaries that return a decent English sentence. It will echo a phrase like:
    1 hour and 2 minutes remaining
    or
    2 hours and 1 minute remaining
    or
    2 hours and 2 minutes remaining

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts


  •  

    Posting Permissions

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