emehrkay Posted March 30, 2006 Share Posted March 30, 2006 i have a sting that is 8:30, i need to add 7.5 hours to it and get 4:00 as the resultso far my thinking has led me here where $date = "8:30" and $dur = 7.5list($hours, $mins) = explode(":", $date);$hours *= 60;$dur *= 60;$total = ($dur + $hours + $mins)/60; //16what do i do next? Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/ Share on other sites More sharing options...
ober Posted March 30, 2006 Share Posted March 30, 2006 Well, 16:00 would be 4:00 PM.... but beyond that, I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22407 Share on other sites More sharing options...
Zane Posted March 30, 2006 Share Posted March 30, 2006 could you just convert it to UNIX timestamp and do a couple date/time functions[code]date("g:i", strtotime("8:30 + 7 hours 30 minutes"));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22409 Share on other sites More sharing options...
lead2gold Posted March 30, 2006 Share Posted March 30, 2006 [!--quoteo(post=360110:date=Mar 30 2006, 02:22 PM:name=emehrkay)--][div class=\'quotetop\']QUOTE(emehrkay @ Mar 30 2006, 02:22 PM) [snapback]360110[/snapback][/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 resultso far my thinking has led me here where $date = "8:30" and $dur = 7.5list($hours, $mins) = explode(":", $date);$hours *= 60;$dur *= 60;$total = ($dur + $hours + $mins)/60; //16what do i do next?[/quote]Obers got the idea:[code]$dur=7.5; // hourslist($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;[/code]Edit: i take to long to work out logic in my head... zanus is the man with the answer :) Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22413 Share on other sites More sharing options...
craygo Posted March 30, 2006 Share Posted March 30, 2006 Best way is to convert everything to seconds then add it up[code]$currenttime = mktime(8, 30, 0); $new = $currenttime + (7*60*60) + (30*60); echo date("h:i:sA",$new);[/code]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 secondsYou 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.RayEdit: ME TOO!! To long to get the code out!! Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22415 Share on other sites More sharing options...
emehrkay Posted March 30, 2006 Author Share Posted March 30, 2006 [!--quoteo(post=360130:date=Mar 30 2006, 03:02 PM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ Mar 30 2006, 03:02 PM) [snapback]360130[/snapback][/div][div class=\'quotemain\'][!--quotec--]could you just convert it to UNIX timestamp and do a couple date/time functions[code]date("g:i", strtotime("8:30 + 7 hours 30 minutes"));[/code][/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) [snapback]360134[/snapback][/div][div class=\'quotemain\'][!--quotec--]Obers got the idea:[code]$dur=7.5; // hourslist($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;[/code]Edit: i take to long to work out logic in my head... zanus is the man with the answer :)[/quote]16:00 am Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22418 Share on other sites More sharing options...
craygo Posted March 30, 2006 Share Posted March 30, 2006 I returned 4:00 for me. Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22421 Share on other sites More sharing options...
emehrkay Posted March 30, 2006 Author Share Posted March 30, 2006 are you using php 5? i am that could be the problem. i did this[code]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;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22426 Share on other sites More sharing options...
craygo Posted March 30, 2006 Share Posted March 30, 2006 i am using PHP 5.1.2. Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22430 Share on other sites More sharing options...
emehrkay Posted March 30, 2006 Author Share Posted March 30, 2006 i cant call it. thanks for all the help fellas, what i just posted seems to "work" for now Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22433 Share on other sites More sharing options...
Zane Posted March 30, 2006 Share Posted March 30, 2006 I wonder if this works...try it[code]strtotime("7 hours 30 minutes", strtotime("8:30"));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6205-adding-times/#findComment-22437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.