forked from cosmocommerce/mtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime.php
116 lines (99 loc) · 2.61 KB
/
DateTime.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* {license_notice}
*
* @category Magento
* @package \Magento\Framework\Stdlib
* @copyright {copyright}
* @license {license_link}
*/
namespace Magento\Framework\Stdlib;
/**
* Converter of date formats
* Internal dates
*/
class DateTime
{
/**#@+
* Date format, used as default. Compatible with \Zend_Date
*/
const DATETIME_INTERNAL_FORMAT = 'yyyy-MM-dd HH:mm:ss';
const DATE_INTERNAL_FORMAT = 'yyyy-MM-dd';
const DATETIME_PHP_FORMAT = 'Y-m-d H:i:s';
const DATE_PHP_FORMAT = 'Y-m-d';
/**#@-*/
/**
* Minimum allowed year value
*/
const YEAR_MIN_VALUE = -10000;
/**
* Maximum allowed year value
*/
const YEAR_MAX_VALUE = 10000;
/**
* Convert date to UNIX timestamp
* Returns current UNIX timestamp if date is true
*
* @param \Magento\Framework\Stdlib\DateTime\DateInterface|bool $date
* @return int
*/
public function toTimestamp($date)
{
if ($date instanceof \Magento\Framework\Stdlib\DateTime\DateInterface) {
return $date->getTimestamp();
}
if ($date === true) {
return time();
}
return strtotime($date);
}
/**
* Retrieve current date in internal format
*
* @param boolean $withoutTime day only flag
* @return string
*/
public function now($withoutTime = false)
{
$format = $withoutTime ? self::DATE_PHP_FORMAT : self::DATETIME_PHP_FORMAT;
return date($format);
}
/**
* Format date to internal format
*
* @param string|\Zend_Date|bool|null $date
* @param boolean $includeTime
* @return string|null
*/
public function formatDate($date, $includeTime = true)
{
if ($date === true) {
return $this->now(!$includeTime);
}
if ($date instanceof \Magento\Framework\Stdlib\DateTime\DateInterface) {
if ($includeTime) {
return $date->toString(self::DATETIME_INTERNAL_FORMAT);
} else {
return $date->toString(self::DATE_INTERNAL_FORMAT);
}
}
if (empty($date)) {
return null;
}
if (!is_numeric($date)) {
$date = $this->toTimestamp($date);
}
$format = $includeTime ? self::DATETIME_PHP_FORMAT : self::DATE_PHP_FORMAT;
return date($format, $date);
}
/**
* Check whether sql date is empty
*
* @param string $date
* @return boolean
*/
public function isEmptyDate($date)
{
return preg_replace('#[ 0:-]#', '', $date) === '';
}
}