1 | <?php |
||
36 | class FormatService |
||
37 | { |
||
38 | const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
39 | |||
40 | /** |
||
41 | * @see http://php.net/manual/de/function.date.php for formatting options |
||
42 | * @param string $input the passed date string |
||
43 | * @param string $inputFormat the input format that should be used for parsing |
||
44 | * @param string $outputFormat The output format, when nothing is passed |
||
45 | * $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] will be used or Y-m-d when nothing is configured |
||
46 | * @param DateTimeZone $timezone |
||
47 | * @return \DateTime|string |
||
48 | */ |
||
49 | 7 | public function format($input = '', $inputFormat = 'Y-m-d\TH:i:s\Z', $outputFormat = '', $timezone = null) |
|
60 | |||
61 | /** |
||
62 | * Converts a date from unix timestamp to ISO 8601 format. |
||
63 | * |
||
64 | * @param int $timestamp unix timestamp |
||
65 | * @return string the date in ISO 8601 format |
||
66 | */ |
||
67 | 65 | public function timestampToIso($timestamp) |
|
71 | |||
72 | /** |
||
73 | * Converts a date from ISO 8601 format to unix timestamp. |
||
74 | * |
||
75 | * @param string $isoTime date in ISO 8601 format |
||
76 | * @return int unix timestamp |
||
77 | */ |
||
78 | 3 | public function isoToTimestamp($isoTime) |
|
79 | { |
||
80 | 3 | $dateTime = \DateTime::createFromFormat(self::SOLR_ISO_DATETIME_FORMAT, |
|
81 | $isoTime); |
||
82 | 3 | return $dateTime ? (int)$dateTime->format('U') : 0; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
||
87 | * |
||
88 | * @param int $timestamp unix timestamp |
||
89 | * @return string the date in ISO 8601 format |
||
90 | */ |
||
91 | 3 | public function timestampToUtcIso($timestamp) |
|
95 | |||
96 | /** |
||
97 | * Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
||
98 | * |
||
99 | * @param string $isoTime date in ISO 8601 format |
||
100 | * @return int unix timestamp |
||
101 | */ |
||
102 | 3 | public function utcIsoToTimestamp($isoTime) |
|
103 | { |
||
104 | 3 | $utcTimeZone = new \DateTimeZone('UTC'); |
|
105 | 3 | $dateTime = \DateTime::createFromFormat(self::SOLR_ISO_DATETIME_FORMAT, |
|
106 | $isoTime, $utcTimeZone); |
||
107 | 3 | return $dateTime ? (int)$dateTime->format('U') : 0; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Applies the formatting using DateTime. |
||
112 | * |
||
113 | * @param string $input |
||
114 | * @param string $inputFormat |
||
115 | * @param string $outputFormat |
||
116 | * @param DateTimeZone $timezone |
||
117 | * @return \DateTime|string |
||
118 | */ |
||
119 | 7 | protected function getFormattedDate($input, $inputFormat, $outputFormat, DateTimeZone $timezone) |
|
128 | } |
||
129 |