Jump to content


Photo

adding times


  • Please log in to reply
10 replies to this topic

#1 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 30 March 2006 - 07:22 PM

i have a sting that is 8:30, i need to add 7.5 hours to it and get 4:00 as the result

so far my thinking has led me here where $date = "8:30" and $dur = 7.5

list($hours, $mins) = explode(":", $date);
$hours *= 60;
$dur *= 60;
$total = ($dur + $hours + $mins)/60; //16

what do i do next?



#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 March 2006 - 07:53 PM

Well, 16:00 would be 4:00 PM.... but beyond that, I'm not sure.

Info: PHP Manual


#3 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 30 March 2006 - 08:02 PM

could you just convert it to UNIX timestamp and do a couple date/time functions

date("g:i", strtotime("8:30 + 7 hours 30 minutes"));

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#4 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 30 March 2006 - 08:16 PM

[!--quoteo(post=360110:date=Mar 30 2006, 02:22 PM:name=emehrkay)--][div class=\'quotetop\']QUOTE(emehrkay @ Mar 30 2006, 02:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i have a sting that is 8:30, i need to add 7.5 hours to it and get 4:00 as the result

so far my thinking has led me here where $date = "8:30" and $dur = 7.5

list($hours, $mins) = explode(":", $date);
$hours *= 60;
$dur *= 60;
$total = ($dur + $hours + $mins)/60; //16

what do i do next?
[/quote]

Obers got the idea:


$dur=7.5; // hours

list($hours, $mins) = explode(":", $date);
$hours *=  60; // convert to minutes
$mins +=$hours; // add minues
$tempdur = $dur*60; // convert to minutes
$total = $tempdur + $mins; // tally it together

/* convert back to time */
$hours = ($total/60);    // get hours
$mins  = ($total%60);  // get min

$ampm = (($hours/12)==1)?"pm":"am";
$hours =   (($hours/12)==1)?($hours - 12):$hours;

// now just format it back in a string:
$strformat = sprintf("%02d:%02d %s", $hours,$mins,$ampm);

echo $strformat;

Edit: i take to long to work out logic in my head... zanus is the man with the answer :)

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 March 2006 - 08:18 PM

Best way is to convert everything to seconds then add it up

$currenttime = mktime(8, 30, 0); 
$new =  $currenttime + (7*60*60) + (30*60); 
echo date("h:i:sA",$new);

Here is breakdown

(7*60*60) this is 7 hours in seconds 7 hours * 60 minutes * 60 seconds
(30 * 60) this is 30 minutes in seconds 30 minutes * 60 seconds

You could go as far and making them into variables in a form and passing them on.

The best thing about doing it this way is that it will calculate even if you use 24 hour time or 12 hour time and at any part of the day. if you have a time of 22:00 adding 7.5 to id will not give you 5:30.

Ray

Edit: ME TOO!! To long to get the code out!!

#6 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 30 March 2006 - 08:49 PM

[!--quoteo(post=360130:date=Mar 30 2006, 03:02 PM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ Mar 30 2006, 03:02 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
could you just convert it to UNIX timestamp and do a couple date/time functions

date("g:i", strtotime("8:30 + 7 hours 30 minutes"));
[/quote]
that returns 11:00, why is this so difficult

[!--quoteo(post=360134:date=Mar 30 2006, 03:16 PM:name=lead2gold)--][div class=\'quotetop\']QUOTE(lead2gold @ Mar 30 2006, 03:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Obers got the idea:


$dur=7.5; // hours

list($hours, $mins) = explode(":", $date);
$hours *=  60; // convert to minutes
$mins +=$hours; // add minues
$tempdur = $dur*60; // convert to minutes
$total = $tempdur + $mins; // tally it together

/* convert back to time */
$hours = ($total/60);    // get hours
$mins  = ($total%60);  // get min

$ampm = (($hours/12)==1)?"pm":"am";
$hours =   (($hours/12)==1)?($hours - 12):$hours;

// now just format it back in a string:
$strformat = sprintf("%02d:%02d %s", $hours,$mins,$ampm);

echo $strformat;

Edit: i take to long to work out logic in my head... zanus is the man with the answer :)
[/quote]

16:00 am


#7 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 March 2006 - 08:49 PM

I returned 4:00 for me.

#8 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 30 March 2006 - 08:53 PM

are you using php 5? i am that could be the problem.

i did this
list($hours, $mins) = explode(":", $date);
            $hours *= 60;
            $dur   *= 60;
            $total = ($dur + $hours + $mins);
            
            $hours = ($total/60);    
            $mins  = ($total%60); 
            if($hours > 12) $hours -= 12;
            if($mins < 10) $mins .= "0";
            
             
            return $hours.":".$mins;


#9 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 March 2006 - 09:00 PM

i am using PHP 5.1.2.

#10 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 30 March 2006 - 09:07 PM

i cant call it. thanks for all the help fellas, what i just posted seems to "work" for now

#11 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 30 March 2006 - 09:27 PM

I wonder if this works...try it
strtotime("7 hours 30 minutes", strtotime("8:30"));

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users