Hi,

I am wanting to Calculate Hours & Minutes between two Dates/Time.

I have four fields: ss_datestart, ss_dateend and ss_timestart and ss_timeend.

I want to display the hours and minutes between two dates/times.

Example:

\$ss_datestart \$ss_timestart --- \$ss_dateend \$ss_timeend
05-12-2009 13:44:31 --- 05-12-2009 17:55:31

I want it to then display the difference in hours and minutes only, so it would return: 04h 11m (ignoring the seconds of course).

And if it were something that went over a day so:

05-12-2009 13:44:31 --- 06-12-2009 17:55:31 <--- This would return: 28h 11m

FYI: Dates are dd-mm-yyyy

## All 10 Replies

It would be easier if you could combine your ss_datestart and ss_timestart columns into one DATETIME column (and similar for end date and time), but you could do something like this in php:

``````// all timestamps are in seconds, so define the number of seconds in one hour
define("SECONDS_PER_HOUR", 60*60);
// get the data from the database
\$result = mysql_query("select ss_datestart, ss_timestart, ss_dateend, ss_timeend from datetimetable") or die(mysql_error());
while(\$row = mysql_fetch_assoc(\$result))
{
// calculate the start timestamp
\$startdatetime = strtotime(\$row["ss_datestart"] . " " . \$row["ss_timestart"]);
// calculate the end timestamp
\$enddatetime = strtotime(\$row["ss_dateend"] . " " . \$row["ss_timeend"]);
// calulate the difference in seconds
\$difference = \$enddatetime - \$startdatetime;
// hours is the whole number of the division between seconds and SECONDS_PER_HOUR
\$hoursDiff = \$difference / SECONDS_PER_HOUR;
// and the minutes is the remainder
\$minutesDiffRemainder = \$difference % SECONDS_PER_HOUR;
// output the result
echo \$hoursDiff . "h " . \$minutesDiffRemainder . "m";
}``````

You may need to fiddle a bit, but this code should point you in the right direction.

Hi,

I am wanting to Calculate Hours & Minutes between two Dates/Time.

I have four fields: ss_datestart, ss_dateend and ss_timestart and ss_timeend.

I want to display the hours and minutes between two dates/times.

Example:

\$ss_datestart \$ss_timestart --- \$ss_dateend \$ss_timeend
05-12-2009 13:44:31 --- 05-12-2009 17:55:31

I want it to then display the difference in hours and minutes only, so it would return: 04h 11m (ignoring the seconds of course).

And if it were something that went over a day so:

05-12-2009 13:44:31 --- 06-12-2009 17:55:31 <--- This would return: 28h 11m

FYI: Dates are dd-mm-yyyy

just make sure the times are made with time() then use the stuff on whatever page
to like do the calculations
its in seconds so its easy

It would be easier if you could combine your ss_datestart and ss_timestart columns into one DATETIME column (and similar for end date and time), but you could do something like this in php:

``````// all timestamps are in seconds, so define the number of seconds in one hour
define("SECONDS_PER_HOUR", 60*60);
// get the data from the database
\$result = mysql_query("select ss_datestart, ss_timestart, ss_dateend, ss_timeend from datetimetable") or die(mysql_error());
while(\$row = mysql_fetch_assoc(\$result))
{
// calculate the start timestamp
\$startdatetime = strtotime(\$row["ss_datestart"] . " " . \$row["ss_timestart"]);
// calculate the end timestamp
\$enddatetime = strtotime(\$row["ss_dateend"] . " " . \$row["ss_timeend"]);
// calulate the difference in seconds
\$difference = \$enddatetime - \$startdatetime;
// hours is the whole number of the division between seconds and SECONDS_PER_HOUR
\$hoursDiff = \$difference / SECONDS_PER_HOUR;
// and the minutes is the remainder
\$minutesDiffRemainder = \$difference % SECONDS_PER_HOUR;
// output the result
echo \$hoursDiff . "h " . \$minutesDiffRemainder . "m";
}``````

You may need to fiddle a bit, but this code should point you in the right direction.

SLIGHT CORRECTION: (these are obviously my variables, using correct DATETIME stamp in mysql)

``````\$difference = strtotime(\$list_vids['ExpirationDate']) - strtotime(\$now);
\$hours = \$difference / 3600; // 3600 seconds in an hour
\$minutes = (\$hours - floor(\$hours)) * 60;
\$final_hours = round(\$hours,0);
\$final_minutes = round(\$minutes);``````

Seems to work for me, outputs "#Hours hours #minutes minutes"

how about if I wanted the result in a timestamp

u cant have the difference in a timestamp. u can have the first timestamp and the second one, but u cant have the difference. the difference u have to calculate into the days months years things

***I apologize this will ONLY work with php > 5.3.0***

Would not a better solution be to use the tools that PHP already provides for doing just this?

``````\$datetime1 = new DateTime(\$row["ss_datestart"] . " " . \$row["ss_timestart"]);
\$datetime2 = new DateTime(\$row["ss_dateend"] . " " . \$row["ss_timeend"]);
\$interval = \$datetime1->diff(\$datetime2);
echo \$interval->format('%Y years, %M months, %D days, %I minutes, %S seconds');``````

Few things to pay attention to.

If you need to convert between timezones, that is provide by the second optional parameter to new DateTime().
DateTime (http://www.php.net/manual/en/class.datetime.php) class is available since php 5.2.0

Formatting information can be found here: http://www.php.net/manual/en/dateinterval.format.php

I DID NOT test this code. I believe the input format should be okay but it may need adjusted depending on how it come from the database.

This works just fine for me.

The Output is 1:30;

``````\$date = '07-11-1988';
\$beggin_hour = '13:30';
\$end_hour = '14:00';

// Hours
define("SECONDS_PER_HOUR", 60*60);
// Calculate timestamp
\$start = strtotime(\$date." ".\$beggin_hour);
\$stop = strtotime(\$date." ".\$end_hour);

// Diferences
\$difference = \$stop - \$start;

// Hours
\$hours = round(\$difference / SECONDS_PER_HOUR, 0, PHP_ROUND_HALF_DOWN);

// Minutes
\$minutes = (\$difference % SECONDS_PER_HOUR) / 60;

echo \$hours. ":" .\$minutes;``````

Sorry for taking some code from you guys. I posted this in the hope that everybody can enjoy it.

Hi,

I am wanting to Calculate Hours & Minutes between two Dates/Time
i have 2 grop down list time as in the code and Iwont to Calculate the time between the two Dates/Time
this my code

``````<tr valign="top">
<td class="title" ><font face="Verdana, Geneva, sans-serif" color="#FFFFFF">Time</font></td></tr>
<tr><td class="field"  valign="top">

<select name="time_from" size="1" id="search_pickup_time" style="color:#666666">
<option value=''>Select Time</option>
<option value="12:00"><b>NOON</b></option>
<option value="12:30 PM">12:30 PM</option>
<option value="13:00 PM">1:00 PM</option>
<option value="13:30 PM">1:30 PM</option>
<option value="14:00 PM">2:00 PM</option>
<option value="14:30 PM">2:30 PM</option>
<option value="15:00 PM">3:00 PM</option>
<option value="15:30 PM">3:30 PM</option>
<option value="16:00 PM">4:00 PM</option>
<option value="16:30 PM">4:30 PM</option>
<option value="17:00 PM">5:00 PM</option>
<option value="17:30 PM">5:30 PM</option>
<option value="18:00 PM">6:00 PM</option>
<option value="18:30 PM">6:30 PM</option>
<option value="19:00 PM">7:00 PM</option>
<option value="19:30 PM">7:30 PM</option>
<option value="20:00 PM">8:00 PM</option>
<option value="20:30 PM">8:30 PM</option>
<option value="21:00 PM">9:00 PM</option>
<option value="21:30 PM">9:30 PM</option>
<option value="22:00 PM">10:00 PM</option>
<option value="22:30 PM">10:30 PM</option>
<option value="23:00 PM">11:00 PM</option>
<option value="23:30 PM">11:30 PM</option>
<option value="00:00"><b>MIDNIGHT</b></option>
<option value="00:30 AM">12:30 AM</option>
<option value="01:00 AM">1:00 AM</option>
<option value="01:30">1:30 AM</option>
<option value="02:00 AM">2:00 AM</option>
<option value="02:30 AM">2:30 AM</option>
<option value="03:00 AM">3:00 AM</option>
<option value="03:30 AM">3:30 AM</option>
<option value="04:00 AM">4:00 AM</option>
<option value="04:30 AM">4:30 AM</option>
<option value="05:00 AM">5:00 AM</option>
<option value="05:30 AM">5:30 AM</option>
<option value="06:00 AM">6:00 AM</option>
<option value="06:30 AM">6:30 AM</option>
<option value="07:00 AM">7:00 AM</option>
<option value="07:30 AM">7:30 AM</option>
<option value="08:00 AM">8:00 AM</option>
<option value="08:30 AM">8:30 AM</option>
<option value="09:00 AM">9:00 AM</option>
<option value="09:30 AM">9:30 AM</option>
<option value="10:00 AM">10:00 AM</option>
<option value="10:30 AM">10:30 AM</option>
<option value="11:00 AM">11:00 AM</option>
<option value="11:30 AM">11:30 AM</option>
</select>
<font face="Verdana, Geneva, sans-serif" color="#FFFFFF"> to</font>
<select id="search_drop_off_time" name="time_to" style="color:#666666">
<option value=''>Select Time</option>
<option value="12:00"><b>NOON</b></option>
<option value="12:30 PM">12:30 PM</option>
<option value="13:00 PM">1:00 PM</option>
<option value="13:30 PM">1:30 PM</option>
<option value="14:00 PM">2:00 PM</option>
<option value="14:30 PM">2:30 PM</option>
<option value="15:00 PM">3:00 PM</option>
<option value="15:30 PM">3:30 PM</option>
<option value="16:00 PM">4:00 PM</option>
<option value="16:30 PM">4:30 PM</option>
<option value="17:00 PM">5:00 PM</option>
<option value="17:30 PM">5:30 PM</option>
<option value="18:00 PM">6:00 PM</option>
<option value="18:30 PM">6:30 PM</option>
<option value="19:00 PM">7:00 PM</option>
<option value="19:30 PM">7:30 PM</option>
<option value="20:00 PM">8:00 PM</option>
<option value="20:30 PM">8:30 PM</option>
<option value="21:00 PM">9:00 PM</option>
<option value="21:30 PM">9:30 PM</option>
<option value="22:00 PM">10:00 PM</option>
<option value="22:30 PM">10:30 PM</option>
<option value="23:00 PM">11:00 PM</option>
<option value="23:30 PM">11:30 PM</option>
<option value="00:00"><b>MIDNIGHT</b></option>
<option value="00:30 AM">12:30 AM</option>
<option value="01:00 AM">1:00 AM</option>
<option value="01:30">1:30 AM</option>
<option value="02:00 AM">2:00 AM</option>
<option value="02:30 AM">2:30 AM</option>
<option value="03:00 AM">3:00 AM</option>
<option value="03:30 AM">3:30 AM</option>
<option value="04:00 AM">4:00 AM</option>
<option value="04:30 AM">4:30 AM</option>
<option value="05:00 AM">5:00 AM</option>
<option value="05:30 AM">5:30 AM</option>
<option value="06:00 AM">6:00 AM</option>
<option value="06:30 AM">6:30 AM</option>
<option value="07:00 AM">7:00 AM</option>
<option value="07:30 AM">7:30 AM</option>
<option value="08:00 AM">8:00 AM</option>
<option value="08:30 AM">8:30 AM</option>
<option value="09:00 AM">9:00 AM</option>
<option value="09:30 AM">9:30 AM</option>
<option value="10:00 AM">10:00 AM</option>
<option value="10:30 AM">10:30 AM</option>
<option value="11:00 AM">11:00 AM</option>
<option value="11:30 AM">11:30 AM</option>
</select></td>

</tr>``````