Jump to content

adding times


emehrkay

Recommended Posts

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?

Link to comment
Share on other sites

[!--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 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:

[code]

$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;
[/code]

Edit: i take to long to work out logic in my head... zanus is the man with the answer :)
Link to comment
Share on other sites

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 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!!
Link to comment
Share on other sites

[!--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; // 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;
[/code]

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

16:00 am
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.