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
https://forums.phpfreaks.com/topic/116414-caculating-time-in-php/
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;
}

?>

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.

 

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.

 

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.

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)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.