Skip to content

Instantly share code, notes, and snippets.

@ksnider
Last active December 30, 2015 09:28
Show Gist options
  • Select an option

  • Save ksnider/7809035 to your computer and use it in GitHub Desktop.

Select an option

Save ksnider/7809035 to your computer and use it in GitHub Desktop.
This is a script which demonstrates the use of PHP script to find the difference between two dates and/or times.
<html>
<head>
<title>TimeDiff Calculator</title>
</head>
<body>
<!-- this tells the browser where and how to send the data from the form -->
<form action='timesheet-calc.php' method='post' enctype="multipart/form-data">
Start : <input type='text' name='start'><br />
End : <input type='text' name='end'><br /><br/>
<input type='submit' value='Submit'>
</form>
</body>
</html>
<?php
/**
* Format an interval to show all existing components.
* If the interval doesn't have a time component (years, months, etc)
* That component won't be displayed.
*
* @param DateInterval $interval The interval
*
* @return string Formatted interval string.
*/
function format_interval(DateInterval $interval) {
$result = "";
if ($interval->y) { $result .= $interval->format("%y years "); }
if ($interval->m) { $result .= $interval->format("%m months "); }
if ($interval->d) { $result .= $interval->format("%d days "); }
if ($interval->h) { $result .= $interval->format("%h hours "); }
if ($interval->i) { $result .= $interval->format("%i minutes "); }
if ($interval->s) { $result .= $interval->format("%s seconds "); }
return $result;
}
echo $start = $_POST['start'];
echo "<br/>";
echo $end = $_POST['end'];
echo "<br/><br/>";
$first_date = new DateTime($start);
$second_date = new DateTime($end);
$difference = $first_date->diff($second_date);
echo "Difference is " . format_interval($difference);
?>
<br/><br/>
NOTE: Any of this can be written to Infusionsoft or used to apply a tag, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment