| 1 |
<?php
|
| 2 |
|
| 3 |
class DateValue {
|
| 4 |
protected $date;
|
| 5 |
protected $defaultFormat = 'r';
|
| 6 |
|
| 7 |
public function __construct($date = NULL, DateTimeZone $zone = NULL) {
|
| 8 |
if ($date instanceof DateTime) {
|
| 9 |
$this->date = $date;
|
| 10 |
}
|
| 11 |
else {
|
| 12 |
if (!isset($zone)) {
|
| 13 |
$zone = new DateTimeZone(date_default_timezone_get());
|
| 14 |
}
|
| 15 |
$this->date = new DateTime($date, $zone);
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
public function setDefaultFormat($format) {
|
| 20 |
$this->defaultFormat = $format;
|
| 21 |
}
|
| 22 |
|
| 23 |
public function format($format = NULL) {
|
| 24 |
if (empty($format)) {
|
| 25 |
$format = $this->defaultFormat;
|
| 26 |
}
|
| 27 |
return $this->date->format($format);
|
| 28 |
}
|
| 29 |
|
| 30 |
public function __toString() {
|
| 31 |
return $this->format();
|
| 32 |
}
|
| 33 |
|
| 34 |
public function calc($modification) {
|
| 35 |
$new_date = clone($this->date);
|
| 36 |
$new_date->modify($modification);
|
| 37 |
return new self($new_date);
|
| 38 |
}
|
| 39 |
|
| 40 |
public function getOffset() {
|
| 41 |
return $this->date->getOffset();
|
| 42 |
}
|
| 43 |
|
| 44 |
public function getTimezone() {
|
| 45 |
return $this->date->getTimezone();
|
| 46 |
}
|
| 47 |
|
| 48 |
public function setTimezone(DateTimeZone $zone) {
|
| 49 |
$new_date = clone($this->date);
|
| 50 |
$new_date->setTimezone($zone);
|
| 51 |
return new self($new_date);
|
| 52 |
}
|
| 53 |
|
| 54 |
public function setISODate($year, $week, $day = NULL) {
|
| 55 |
$new_date = clone($this->date);
|
| 56 |
$new_date->setISODate($year, $week, $day);
|
| 57 |
return new self($new_date);
|
| 58 |
}
|
| 59 |
|
| 60 |
public function setEpoc($timestamp) {
|
| 61 |
$new_date = new DateTime('@'. $timestamp);
|
| 62 |
return new self($new_date);
|
| 63 |
}
|
| 64 |
|
| 65 |
public function setDate($year = NULL, $month = NULL, $day = NULL) {
|
| 66 |
$new_date = clone($this->date);
|
| 67 |
if (!isset($year)) {
|
| 68 |
$year = $new_date->format('Y');
|
| 69 |
}
|
| 70 |
if (!isset($month)) {
|
| 71 |
$month = $new_date->format('n');
|
| 72 |
}
|
| 73 |
if (!isset($day)) {
|
| 74 |
$day = $new_date->format('d');
|
| 75 |
}
|
| 76 |
$new_date->setDate($year, $month, $day);
|
| 77 |
return new self($new_date);
|
| 78 |
}
|
| 79 |
|
| 80 |
public function setYear($year) {
|
| 81 |
$new_date = clone($this->date);
|
| 82 |
$new_date->setDate($year, $new_date->format('n'), $new_date->format('d'));
|
| 83 |
return new self($new_date);
|
| 84 |
}
|
| 85 |
|
| 86 |
public function setMonth($month) {
|
| 87 |
$new_date = clone($this->date);
|
| 88 |
$new_date->setDate($new_date->format('Y'), $month, $new_date->format('d'));
|
| 89 |
return new self($new_date);
|
| 90 |
}
|
| 91 |
|
| 92 |
public function setDay($day) {
|
| 93 |
$new_date = clone($this->date);
|
| 94 |
$new_date->setDate($new_date->format('Y'), $new_date->format('n'), $day);
|
| 95 |
return new self($new_date);
|
| 96 |
}
|
| 97 |
|
| 98 |
public function setTime($hour = NULL, $minute = NULL, $second = NULL) {
|
| 99 |
$new_date = clone($this->date);
|
| 100 |
if (!isset($hour)) {
|
| 101 |
$hour = $new_date->format('G');
|
| 102 |
}
|
| 103 |
if (!isset($minute)) {
|
| 104 |
$minute = $new_date->format('i');
|
| 105 |
}
|
| 106 |
if (!isset($second)) {
|
| 107 |
$second = $new_date->format('s');
|
| 108 |
}
|
| 109 |
$new_date->setTime($hour, $minute, $second);
|
| 110 |
return new self($new_date);
|
| 111 |
}
|
| 112 |
|
| 113 |
public function setHour($hour) {
|
| 114 |
$new_date = clone($this->date);
|
| 115 |
$new_date->setTime($hour, $new_date->format('i'), $new_date->format('s'));
|
| 116 |
return new self($new_date);
|
| 117 |
}
|
| 118 |
|
| 119 |
public function setMinute($minute) {
|
| 120 |
$new_date = clone($this->date);
|
| 121 |
$new_date->setTime($new_date->format('G'), $minute, $new_date->format('s'));
|
| 122 |
return new self($new_date);
|
| 123 |
}
|
| 124 |
|
| 125 |
public function setSecond($second) {
|
| 126 |
$new_date = clone($this->date);
|
| 127 |
$new_date->setTime($new_date->format('G'), $new_date->format('i'), $second);
|
| 128 |
return new self($new_date);
|
| 129 |
}
|
| 130 |
|
| 131 |
public function compare(self $other) {
|
| 132 |
return self::compareDates($this, $other);
|
| 133 |
}
|
| 134 |
|
| 135 |
public static function compareDates(self $date1, self $date2) {
|
| 136 |
return strcmp(
|
| 137 |
$date1->setTimezone(new DateTimeZone('UTC'))->format(DateTime::ISO8601),
|
| 138 |
$date2->setTimezone(new DateTimeZone('UTC'))->format(DateTime::ISO8601)
|
| 139 |
);
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
$date = new DateValue();
|
| 145 |
print $date . PHP_EOL;
|
| 146 |
print $date->calc('+1 week') . PHP_EOL;
|
| 147 |
print $date . PHP_EOL;
|
| 148 |
|
| 149 |
$date2 = $date->calc('+1 week');
|
| 150 |
print $date->compare($date2) . PHP_EOL;
|
| 151 |
print DateValue::compareDates($date, $date2) . PHP_EOL;
|
| 152 |
|
| 153 |
print $date->setYear(2006)->setMonth(12)->setDay(31)->setHour(14)->setMinute(4)->setSecond(20) . PHP_EOL;
|
| 154 |
|
| 155 |
//print $date->setDate()
|
| 156 |
|
| 157 |
|