PHP Function for Calculating End Time from 4-Digit Start Time and Duration
February 24, 2025
One of the functions necessary for the Tablepress Simple TV Schedule Shortcode Extension accepts a 4-digit start time and an episode duration as arguments, returning an array containing a 4-digit end time, as well as the episode’s start and end time in a user-readable format. The math involved is fairly simple, mixed with string operations to accomplish the task.
function calculatestartendtimes( $starttime,$duration) { // Parse the start time from 24-hour (hhmm) to hour and minutes $starttimehours = substr($starttime,0,2); $begintimehours = absint($starttimehours); $starttimeminutes = substr($starttime,2,2); $starttimeminutes = sprintf("%02s", $starttimeminutes); if (($begintimehours > 23) or ($starttimeminutes > 59)) { // return errors for incorrect start time $endtime = '0000'; $begintimedisplay = 'Incorrect start time'; $endtimedisplay = 'Incorrect start time'; } else { // ensure start time is 4-digit for display $starttime = sprintf("%04s", $starttime); // Identify the meridian if ($begintimehours > 11) { if ($begintimehours > 12) { // convert to 12-hour time $begintimehours = $begintimehours - 12; } $meridianbegin = 'pm'; } else { $meridianbegin = 'am'; } if ($begintimehours == 0) { $begintimehours = 12; } // calculate start minutes since midnight $startsincemidnight = 0; $startsincemidnight = intval($starttimehours) * 60; $startsincemidnight = $startsincemidnight + $starttimeminutes; // add duration $endsincemidnight = $startsincemidnight + intval($duration); // set 4-digit result // remainder = dividend % divisor; // quotient = (dividend - remainder) / divisor; $endtimemn = intval($endsincemidnight) % 60; // modulus operator $endtimemn = sprintf("%02s", $endtimemn); $endtimehr = ($endsincemidnight - $endtimemn) / 60; // account for midnight transition If ($endtimehr > 23) { $endtimehr = $endtimehr - 24; } // concatenate the result, and set to 4-digit $endtime = $endtimehr . $endtimemn; $endtime = sprintf("%04s", $endtime); // Identify the meridian if ($endtimehr > 11) { if ($endtimehr > 12) { // convert to 12-hour time $endtimehr = $endtimehr - 12; } $meridianend = 'pm'; } else { $meridianend = 'am'; } if ($endtimehr == 0) { $endtimehr = 12; } $begintimedisplay = strval( $begintimehours ) . ':' . $starttimeminutes . $meridianbegin; $endtimedisplay = strval( $endtimehr ) . ':' . $endtimemn . $meridianend; } return [$endtime, $begintimedisplay, $endtimedisplay]; }
Usage then becomes relatively straightforward:
$calculationarray = calculatestartendtimes( $timestart, $duration); // Set the start time to the previous end time // for the beginning of the next loop $timestart = $calculationarray[0]; // this episode's end time $begintimedisplay = $calculationarray[1]; $endtimedisplay = $calculationarray[2];
This function is used to calculate the start and end times for every episode / Tablepress table in the Tablepress Simple TV Schedule Shortcode Extension, and can be used with most arbitrary but correct values.