timecount

Computes multiple time ranges (see example).

Source code (hide)

Global files are not shown below.

Table of contents
  1. index.php

index.php

back to toc
<?php
/* globals, templates */
$locals = array (
    
'title'       => 'timecount',
    
'description' => 'Computes multiple time ranges (see <a href="index.php?example=1" title="example ranges">example</a>).',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

/* input */
$input = array(
    
'text'       => $_POST['text'],
    
'example'    => $_GET['example'],
    
'hourFormat' => $_POST['hourFormat']
);
if(
$input['example']) {
    
$input = array(
        
'text'       => "03:37 to 03:46\n22:42 to 00:50\n00:00 to 00:01\n02:40 to 05:16\n00:00 to 00:00\n07:15 to 08:11\n20:53 to 21:07",
        
'hourFormat' => true
    
);
}

if(
$input['text']) {
    if(empty(
$input['text']) or !preg_match('#\d:\d\d.*\d:\d\d#mi',$input['text'])) {
        echo 
'<p class="fail">Please provide valid time ranges.</p>';
    }
    else {
        
/* prepare array */
        
$ranges explode("\n",$input['text']);
        
        
/* calculate */
        
$count count($ranges);
        for(
$i=0;$i<$count;$i++) {
            
/* get units */
            // hours
            
$hourStart preg_replace('/^.*?(\d{1,2}):.*$/',    '$1',$ranges[$i]);
            
$hourEnd   preg_replace('/^.*?:.*?(\d{1,2}):.*$/','$1',$ranges[$i]);
            if(
$hourEnd<$hourStart) {
                
$hourEnd .= 24// compensate for day wrapping
            
}

            
// minutes
            
$minuteStart preg_replace('/^.*?:(\d{1,2}).*$/',    '$1',$ranges[$i]);
            
$minuteEnd   preg_replace('/^.*?:.*?:(\d{1,2}).*$/','$1',$ranges[$i]);

            
/* calculate */
            
$result $result + (60*($hourEnd $hourStart)) + ($minuteEnd $minuteStart);
        }

        
/* output as "00h00" or "000 minutes" depending on user config */
        
if($input['hourFormat']) {
            
// calculate
            
$hours floor($result/60);
            
$minutes $result - ($hours*60);

            
// format output
            
$output str_pad($hours,2,'0',STR_PAD_LEFT) . 'h' str_pad($minutes,2,'0',STR_PAD_LEFT);
        }
        else {
            
$output $result ' minutes';
        }
        
        
/* output */
        
echo '<p class="success">The ranges total <strong>' $output '</strong>.</p>';
    }
}

/* debug */
gDebug(get_defined_vars());

/* input form */
?>
<h2>Input form</h2>
<form action="index.php" method="post">
    <h3>Input form</h3>
    <p class="instruction">Type in time ranges in 24-hour format, with anything non-numerical between each time and one range per line. For example, "10:37 to 16:22".</p>
    <textarea
        id="text"
        name="text"
        rows="12"
        cols="12"><?php echo htmlspecialchars($input['text']); ?></textarea><br />
    <input
        type="checkbox"
        id="hourFormat"
        name="hourFormat"
        <?php gCheckBox($input['hourFormat']); ?>
    /> <label for="hourFormat">output hours as 00h00 (instead of "000 minutes").</label><br />
    <?php gDebugOption(); ?>
    <input type="submit" value="Calculate" /><br />
    <input type="reset"  value="Reset form" />
</form>

<?php
/* globals, templates */
makeFooter();
?>

Input form

Input form

Type in time ranges in 24-hour format, with anything non-numerical between each time and one range per line. For example, "10:37 to 16:22".





This tool is written and copyright by Jesse Plamondon-Willard (Pathoschild). You may freely use, distribute, and modify this script in any way and for any purpose, so long as you cite the above name as original author.