1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96:
<?php
namespace Donut;
class Time
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
public function isOlderThan(Time $time)
{
return $this->value < $time->getValue();
}
public function getMinutesTo(Time $time)
{
$start = $this->getDateTime();
$end = $time->getDateTime();
$diff = $start->diff($end);
return ($diff->y * 365 * 24 * 60)
+ ($diff->m * 30 * 24 * 60)
+ ($diff->d * 24 * 60)
+ ($diff->h * 60)
+ ($diff->i);
}
public function getDateTime()
{
return new \DateTime($this->value, new \DateTimeZone('UTC'));
}
public static function create($date)
{
if (is_int($date)) {
$date = new \DateTime('@' . $date, new \DateTimeZone('UTC'));
}
if ($date instanceof \DateTime) {
return new static($date->format('Y-m-d H:i:s'));
}
$date = new \DateTime($date, new \DateTimeZone('UTC'));
$date->setTimeZone(new \DateTimeZone('UTC'));
return new static($date->format('Y-m-d H:i:s'));
}
public static function sub(Time $time, $minutes)
{
$datetime = $time->getDateTime();
$datetime->sub(new \DateInterval('PT' . abs((int) $minutes) . 'M'));
return new static($datetime->format('Y-m-d H:i:s'));
}
}