Open Journal Systems  3.3.0
DateTime.php
1 <?php
2 /*
3  * citeproc-php
4  *
5  * @link http://github.com/seboettg/citeproc-php for the source repository
6  * @copyright Copyright (c) 2017 Sebastian Böttger.
7  * @license https://opensource.org/licenses/MIT
8  */
9 
11 
12 use DateTimeZone;
13 use Exception;
15 
16 class DateTime extends \DateTime
17 {
21  private $year = 0;
22 
26  private $month = 0;
27 
31  private $day = 0;
32 
40  public function __construct($year, $month, $day)
41  {
42  try {
43  parent::__construct("$year-$month-$day", new DateTimeZone("Europe/Berlin"));
44  } catch (Exception $e) {
45  throw new InvalidDateTimeException("Could not create valid date with year=$year, month=$month, day=$day.");
46  }
47 
48  $this->year = intval(self::format("Y"));
49  $this->month = intval(self::format("n"));
50  $this->day = intval(self::format("j"));
51  }
52 
57  public function setYear($year)
58  {
59  $this->year = $year;
60  return $this;
61  }
62 
67  public function setMonth($month)
68  {
69  $this->month = $month;
70  return $this;
71  }
72 
77  public function setDay($day)
78  {
79  $this->day = $day;
80  return $this;
81  }
82 
89  public function setDate($year, $month, $day)
90  {
91  $this->year = $year;
92  $this->month = $month;
93  $this->day = $day;
94  parent::setDate($year, $month, $day);
95  return $this;
96  }
97 
101  public function getArray()
102  {
103  return [$this->year, $this->month, $this->day];
104  }
105 
109  public function getYear()
110  {
111  return $this->year;
112  }
113 
117  public function getMonth()
118  {
119  return $this->month;
120  }
121 
125  public function getDay()
126  {
127  return $this->day;
128  }
129 
133  public function renderNumeric()
134  {
135  $ret = $this->year;
136  $ret .= $this->month > 0 && $this->month < 13 ? "-".sprintf("%02s", $this->month) : "";
137  $ret .= $this->day > 0 && $this->day < 32 ? "-".sprintf("%02s", $this->day) : "";
138  return $ret;
139  }
140 }
Seboettg\CiteProc\Rendering\Date\DateTime\setDate
setDate($year, $month, $day)
Definition: DateTime.php:98
Seboettg\CiteProc\Rendering\Date\DateTime\getDay
getDay()
Definition: DateTime.php:134
Seboettg\CiteProc\Rendering\Date\DateTime\renderNumeric
renderNumeric()
Definition: DateTime.php:142
Seboettg\CiteProc\Exception\InvalidDateTimeException
Definition: InvalidDateTimeException.php:10
Seboettg\CiteProc\Rendering\Date\DateTime\__construct
__construct($year, $month, $day)
Definition: DateTime.php:49
Seboettg\CiteProc\Rendering\Date\DateTime\setDay
setDay($day)
Definition: DateTime.php:86
Seboettg\CiteProc\Rendering\Date\DateTime\setYear
setYear($year)
Definition: DateTime.php:66
Seboettg\CiteProc\Rendering\Date\DateTime\setMonth
setMonth($month)
Definition: DateTime.php:76
Seboettg\CiteProc\Rendering\Date\DateTime
Definition: DateTime.php:16
Seboettg\CiteProc\Rendering\Date
Definition: Date.php:10
Seboettg\CiteProc\Rendering\Date\DateTime\getMonth
getMonth()
Definition: DateTime.php:126
Seboettg\CiteProc\Rendering\Date\DateTime\getYear
getYear()
Definition: DateTime.php:118
Seboettg\CiteProc\Rendering\Date\DateTime\getArray
getArray()
Definition: DateTime.php:110