<?php
class DateTimeExtended extends DateTime {
public function __get($name) {
switch ($name) {
case 'year':
return (int)$this->format('Y');
case 'month':
return (int)$this->format('n');
case 'day':
return (int)$this->format('j');
case 'hour':
return (int)$this->format('G');
case 'minute':
return (int)$this->format('i');
case 'second':
return (int)$this->format('s');
case 'dayOfWeek':
return (int)$this->format('w');
case 'dayOfYear':
return (int)$this->format('z');
case 'weekOfYear':
return (int)$this->format('W');
case 'daysInMonth':
return (int)$this->format('t');
case 'timestamp':
return (int)$this->format('U');
default:
throw new InvalidArgumentException(sprintf('Unknown property "%s".', $name));
}
}
public static function createFromFormat($format, $time, $tz = null) {
if ($tz) {
$date = parent::createFromFormat($format, $time, $tz);
} else {
$date = parent::createFromFormat($format, $time);
}
$errors = static::getLastErrors();
if ($errors['warning_count'] !== 0 || $errors['error_count'] !== 0) {
throw new InvalidArgumentException(sprintf('Unable to parse date "%s" by pattern "%s".', $time, $format));
}
if (!$date instanceof self) {
return new static($date->format('Y-m-d H:i:s'), $date->getTimeZone());
}
return $date;
}
public function format($format) {
$format = preg_replace_callback('/(?:f|[Мм]есяца?|[Дд]еньнед(?:ели)?)/u', array($this, 'replaceFormatCallback'), $format);
return parent::format($format);
}
public function replaceFormatCallback(array $matches) {
switch ($matches[0]) {
case 'f':
return mb_strtolower(en_get_month_name($this->month));
case 'Месяц':
return ru_get_month_name($this->month);
case 'Месяца':
return ru_get_month_name($this->month, 2);
case 'месяц':
return mb_strtolower(ru_get_month_name($this->month));
case 'месяца':
return mb_strtolower(ru_get_month_name($this->month, 2));
case 'Деньнедели':
return ru_get_weekday_name($this->dayOfWeek);
case 'Деньнед':
return ru_get_weekday_name($this->dayOfWeek, true);
case 'деньнедели':
return mb_strtolower(ru_get_weekday_name($this->dayOfWeek));
case 'деньнед':
return mb_strtolower(ru_get_weekday_name($this->dayOfWeek, true));
}
return $matches[0];
}
public static function convert($from, $to, $value) {
return static::createFromFormat($from, $value)->format($to);
}
public static function check($format, $value) {
try {
static::createFromFormat($format, $value);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
}
}
preferences:
42.2 ms | 402 KiB | 5 Q