1 <?php
2 /* --------------------------------------------------------------
3 EmptyDateTime.inc.php 2016-02-16
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2016 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 /**
13 * EmptyDateTime
14 *
15 * The purpose of this class is to represent a DateTime with support of an empty date 0000-00-00 00:00:00
16 *
17 * @category System
18 * @package Shared
19 * @extends DateTime
20 */
21 class EmptyDateTime extends DateTime
22 {
23 /**
24 * @var bool
25 */
26 protected $isNullDate = false;
27
28
29 /**
30 * @param string $time A date/time string. NULL will be represented as 0000-00-00 00:00:00 instead of
31 * the current time like in DateTime.
32 * @param DateTimeZone $timeZone A DateTimeZone object representing the timezone of $time. If $timezone is omitted,
33 * the current timezone will be used.
34 */
35 public function __construct($time = '0000-01-01 00:00:00', DateTimeZone $timeZone = null)
36 {
37 if(strpos($time, '0000') === 0 || empty($time) || $time === '00.00.0000')
38 {
39 $time = '0000-01-01 00:00:00';
40 }
41
42 if($time === '0000-01-01 00:00:00')
43 {
44 $this->isNullDate = true;
45 }
46
47 parent::__construct($time, $timeZone);
48 }
49
50
51 /**
52 * Formats a date by a given pattern and ensures, that dates that represent empty data are formatted to
53 * '0000-00-00 00:00:00'
54 *
55 * @param string $format
56 *
57 * @return string
58 */
59 public function format($format)
60 {
61 $formattedDate = parent::format($format);
62 if($this->isNullDate)
63 {
64 $formattedDate = preg_replace('/\d/i', '0', $formattedDate);
65 }
66
67 return $formattedDate;
68 }
69 }