Jump to content

Caculating Time in php


dubc07

Recommended Posts

I'm wanting to learn how to calculate time between a start time and an end time in 30 minute increments .

 

$starttime = 600; /////which is militairy time for 6am

 

              /////////////// Wanting to caculate the time between the start and the end

 

$endtime = 1400; ///////which is militairy time for 2pm

 

so the total time from start to end would be 8 hours but i would like the output in minutes, which would be 480.

Is there any way of doing this and HOW.

Thanks.

 

Link to comment
Share on other sites

Wrote a function to do what you need....

 

<?php

// Example

$starttime = 600; /////which is militairy time for 6am
$endtime = 1400; ///////which is militairy time for 2pm 
print give_minutes ($starttime, $endtime);


function give_minutes ($starttime, $endtime)
{
    // Get hours / mins for starttime
    $s_hrs = (int) substr(substr("0".$starttime, -4), 0, 2);
    $s_mins = (int) substr($starttime, -2);
    
    // Get hours / mins for endtime
    $e_hrs = (int) substr(substr("0".$endtime, -4), 0, 2);
    $e_mins = (int) substr($endtime, -2);
    
    // Make the time into timestamps
    $i_starttime = mktime($s_hrs, $s_mins, 0);
    $i_endtime   = mktime($e_hrs, $e_mins, 0);
    
    // Return it
    return ($i_endtime - $i_starttime) / 60;
}

?>

Link to comment
Share on other sites

Not bad, JohnnyThunder. What did you typecast those few vars for?

 

Well, I didn't really need to - it's just habit i've got into for 'completeness' sake.  ;D

 

I think it stems from hack attacks on my websites - typecast everything 'just in case'.

 

Though you're right - PHP would have converted the "00" into a zero anyway.

 

Link to comment
Share on other sites

Now that I actually mess around with the function, I am also curious as to why you nested two substrings in the $s_hrs and $e_hrs, when the same can be accomplished with substr($starttime, 0, 2);

 

 

Link to comment
Share on other sites

The same cannot be accomplished with what you said.  because that value can be one or two digits.

 

I used two substrs, because I needed to split the hours from the minutes.  And because the hours can be one or two digits - I prefixed it with a "0".  Thats the first substring. At this stage, I know the overall size of the time will be 4 characters.  The second substr, was to split the minutes from the hours.

 

Of course, the minutes will always be two digits according to military time.  So I can safely assume it'll always be the last 2 characters of the string.

 

Link to comment
Share on other sites

I believe I was initially confused because I don't understand military time enough to just read it as I see it. :P

 

So, I thought since the end time was 1400, that, it would be 01400 with your substring, then the string returned would have been 01 and not 14 like it should have been.

Link to comment
Share on other sites

It would return 01400 - but i'm grabbing the last four characters of the string (using negative value in the substr command) - which I know will always be good, whether the original is four or five characters.

 

Like this....

 

If they put in 600 as the value, then im doing this...

 

0600

 

then taking the LAST four characters, which will be 0600.  if however, they put in this 1400 to start, then im doing this...

 

01400

 

And still grabbing the last four characters which will be 1400.  it's kindof a way of doing it blind - without knowing what the first characters will be.  (I do however, have to assume 2 characters for the minutes for this to work)

Link to comment
Share on other sites

just so we're all clear, with military time, the function is extreme overkill:

 

$difference = $endtime - $starttime;

$modulo = $difference % 100;

$minutes = (($difference - $modulo) * 0.6) + $modulo;

 

then do what you want with the $minutes.  this is all just math - there should be no string manipulation necessary.  the second line grabs the remainder after dividing by 100 (in the case that the times are not dead on an hour), the third line calculates the number of minutes by multiplying the hours by minutes, and adding the remainder.

 

the reason for 0.6 is that you multiply by 60 (for 60 minutes in one hour) and you divide by 100 (since military time designates the hour in hundreds).  60/100 = 0.6.

 

jonny's method probably works, and should be given credit.  i just think it's good practice not to bother using string manipulation unless you're manipulating strings.

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.