Skip to content

Instantly share code, notes, and snippets.

@renoirb
Created March 17, 2014 00:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/9591913 to your computer and use it in GitHub Desktop.
Save renoirb/9591913 to your computer and use it in GitHub Desktop.
PHP Time Helper
<?php
namespace Renoir\Util;
// Specific
use \DateTime;
use \DateInterval;
/**
* Time helper utility
*
* Takes care of calculating time ranges, intervals.
*
* @author Renoir Boulanger <hello@renoirboulanger.com>
**/
class TimeHelper
{
/**
* Calculate an Interval
*
* Return difference between [now] and [then] as an Interval (object)
*
* @return DateInterval $interval
*/
public function calculateInterval(DateTime $then)
{
$now = new DateTime();
return $now->diff($then);
}
/**
* Difference in days since $then
*
* @return int days
*/
public function differenceDays(DateTime $then)
{
// secs, minutes, hours
return round((($this->differenceSeconds($then) / 60) / 60) / 24, 0, PHP_ROUND_HALF_UP);
}
/**
* Difference in minutes since $then
*
* @return int minutes
*/
public function differenceMinutes(DateTime $then)
{
// secs
return round($this->differenceSeconds($then) / 60, 0, PHP_ROUND_HALF_UP);
}
/**
* Difference in minutes since...?
*
* Source: http://www.php.net/manual/en/dateinterval.format.php, comment by: kuzb on 04-Feb-2011 05:15
*
* @return int minutes
*/
public function differenceSeconds(DateTime $then)
{
$interval = $this->calculateInterval($then);
$seconds = ($interval->y * 365 * 24 * 60 * 60) +
($interval->m * 30 * 24 * 60 * 60) +
($interval->d * 24 * 60 * 60) +
($interval->h * 60 *60) +
$interval->s;
return $seconds;
}
public function toSeconds(DateInterval $interval)
{
return ($interval->y * 365 * 24 * 60 * 60) +
($interval->m * 30 * 24 * 60 * 60) +
($interval->d * 24 * 60 * 60) +
($interval->h * 60 *60) +
($interval->i * 60) +
$interval->s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment