Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2011, 12:43 AM   PM User | #1
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,159
Thanks: 46
Thanked 96 Times in 95 Posts
myfayt can only hope to improve
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 6 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 12-14-2011, 02:18 AM   PM User | #2
>ssp-cdr<
Regular Coder

 
Join Date: May 2007
Posts: 100
Thanks: 16
Thanked 11 Times in 11 Posts
>ssp-cdr< is an unknown quantity at this point
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.
>ssp-cdr< is online now   Reply With Quote
Old 12-14-2011, 10:12 AM   PM User | #3
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 12-14-2011, 12:02 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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
BluePanther is offline   Reply With Quote
Old 12-14-2011, 01:24 PM   PM User | #5
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Expanded a bit and posted a function here:
http://www.codingforums.com/showthread.php?p=1170445
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.