1
|
|
|
<?php |
2
|
|
|
namespace andmemasin\helpers; |
3
|
|
|
|
4
|
|
|
use \DateTime; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A helper class to contain general datetime helper functions |
8
|
|
|
* |
9
|
|
|
* @package app\models\helpers |
10
|
|
|
* @author Tonis Ormisson <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class DateHelper { |
13
|
|
|
|
14
|
|
|
/** @var string End of time for DB logical delete. */ |
15
|
|
|
const END_OF_TIME = '3000-12-31 00:00:00.000000'; |
16
|
|
|
|
17
|
|
|
/** @var DateTime */ |
18
|
|
|
public $now; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->now = new DateTime(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get current time in mysql datetime(6) format as "Y-m-d H:i:s.u" |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getDatetime6(){ |
32
|
|
|
|
33
|
|
|
// TODO this does now consider the getNow() |
34
|
|
|
$t = microtime(true); |
35
|
|
|
$micro = sprintf("%06d",($t - floor($t)) * 1000000); |
36
|
|
|
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) ); |
37
|
|
|
|
38
|
|
|
return $d->format("Y-m-d H:i:s.u"); // note at point on "u" |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* End of time is a default value for time not in reach to mark |
44
|
|
|
* date where we will not reach. Typically default for time_closed value |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getEndOfTime(){ |
48
|
|
|
return self::END_OF_TIME; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $datetime |
54
|
|
|
* @return false|string |
55
|
|
|
*/ |
56
|
|
|
public function getMysqlDateTimeFromDateTime6 ($datetime) { |
57
|
|
|
return date("Y-m-d H:i:s", strtotime( $datetime )); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $dateTime |
63
|
|
|
* @return false|string |
64
|
|
|
* @deprecated use Formatter |
65
|
|
|
*/ |
66
|
|
|
public function getDatetimeForDisplay($dateTime){ |
67
|
|
|
return date("Y-m-d H:i:s", strtotime( $dateTime )); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $sqlDate |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function getDateDifferenceInDays($sqlDate){ |
75
|
|
|
$dStart = $this->now; |
76
|
|
|
$dEnd = new DateTime($sqlDate); |
77
|
|
|
$dDiff = $dStart->diff($dEnd); |
78
|
|
|
$days = $dDiff->days; |
79
|
|
|
return $days; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $dateTime time that we want to check whether it has reached or not |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function hasTimeReached($dateTime) |
87
|
|
|
{ |
88
|
|
|
return $this->now >= new \DateTime($dateTime); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|