1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Vasily Zorin <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPDaemon\Utils; |
7
|
|
|
|
8
|
|
|
class DateTime extends \DateTime |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Support timestamp and available date format |
12
|
|
|
* @param string $time |
13
|
|
|
* @param \DateTimeZone $timezone |
|
|
|
|
14
|
|
|
* @link http://php.net/manual/en/datetime.construct.php |
15
|
|
|
*/ |
16
|
|
|
public function __construct($time = 'now', \DateTimeZone $timezone = null) |
17
|
|
|
{ |
18
|
|
|
if (is_int($time)) { |
19
|
|
|
parent::__construct('now', $timezone); |
20
|
|
|
$this->setTimestamp($time); |
21
|
|
|
} else { |
22
|
|
|
parent::__construct($time, $timezone); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Calculates a difference between two dates |
28
|
|
|
* @see http://www.php.net/manual/en/datetime.diff.php |
29
|
|
|
* @param integer|string|\DateTimeInterface $datetime1 |
30
|
|
|
* @param integer|string|\DateTimeInterface $datetime2 |
31
|
|
|
* @param boolean $absolute |
32
|
|
|
* @return string Something like this: 1 year. 2 mon. 6 day. 4 hours. 21 min. 10 sec. |
33
|
|
|
*/ |
34
|
|
|
public static function diffAsText($datetime1, $datetime2, $absolute = false) |
35
|
|
|
{ |
36
|
|
|
if (!($datetime1 instanceof \DateTimeInterface)) { |
37
|
|
|
$datetime1 = new static($datetime1); |
38
|
|
|
} |
39
|
|
|
if (!($datetime2 instanceof \DateTimeInterface)) { |
40
|
|
|
$datetime2 = new static($datetime2); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$interval = $datetime1->diff($datetime2, $absolute); |
44
|
|
|
$str = ''; |
45
|
|
|
$str .= $interval->y ? $interval->y . ' year. ' : ''; |
46
|
|
|
$str .= $interval->m ? $interval->m . ' mon. ' : ''; |
47
|
|
|
$str .= $interval->d ? $interval->d . ' day. ' : ''; |
48
|
|
|
$str .= $interval->h ? $interval->h . ' hour. ' : ''; |
49
|
|
|
$str .= $interval->i ? $interval->i . ' min. ' : ''; |
50
|
|
|
$str .= $interval->s || $str === '' ? $interval->s . ' sec. ' : ''; |
51
|
|
|
|
52
|
|
|
return rtrim($str); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.