From 253996901a10918e3207d46839cdfdc90d200e72 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 14 Mar 2015 20:53:33 -0400 Subject: Calculate the time spent based on the timetable --- app/Model/DateParser.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app/Model/DateParser.php') diff --git a/app/Model/DateParser.php b/app/Model/DateParser.php index 8a4d3edd..a0d10a36 100644 --- a/app/Model/DateParser.php +++ b/app/Model/DateParser.php @@ -12,6 +12,47 @@ use DateTime; */ class DateParser extends Base { + /** + * Return true if the date is within the date range + * + * @access public + * @param DateTime $date + * @param DateTime $start + * @param DateTime $end + * @return boolean + */ + public function withinDateRange(DateTime $date, DateTime $start, DateTime $end) + { + return $date >= $start && $date <= $end; + } + + /** + * Get the total number of hours between 2 datetime objects + * Minutes are rounded to the nearest quarter + * + * @access public + * @param DateTime $d1 + * @param DateTime $d2 + * @return float + */ + public function getHours(DateTime $d1, DateTime $d2) + { + $seconds = $this->getRoundedSeconds(abs($d1->getTimestamp() - $d2->getTimestamp())); + return round($seconds / 3600, 2); + } + + /** + * Round the timestamp to the nearest quarter + * + * @access public + * @param integer $seconds Timestamp + * @return integer + */ + public function getRoundedSeconds($seconds) + { + return (int) round($seconds / (15 * 60)) * (15 * 60); + } + /** * Return a timestamp if the given date format is correct otherwise return 0 * -- cgit v1.2.3