@ 2013-11-01T07:56:50Z <?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;
}
}
$t = microtime(true);
for ($i = 0; $i < 2000; $i++) {
DateTimeExtended::convert('Y-m-d', 'd.m.Y H:i:s', '2000-01-01');
}
echo 'DateTimeExtended:'.(microtime(true) - $t)."\n";
//----------------------------------------------------------------------------------------------------
function int_pad($number, $count = 2) {
return str_pad((int)$number, $count, '0', STR_PAD_LEFT);
}
function Date_parseDate($format, $str) {
$patterns['Y'] = array('(\d{4})', 'year');
$patterns['y'] = array('(\d{2})');
$patterns['m'] = array('(\d{2})', 'month');
$patterns['n'] = array('(\d{1,2})', 'month');
$patterns['d'] = array('(\d{2})', 'day');
$patterns['j'] = array('(\d{1,2})', 'day');
$patterns['H'] = array('(\d{2})', 'hour');
$patterns['h'] = array('(\d{1,2})', 'hour');
$patterns['i'] = array('(\d{1,2})', 'minute');
$patterns['M'] = array('(\d{2})', 'minute');
$patterns['s'] = array('(\d{1,2})', 'second');
$patterns['U'] = array('(-?\d+)');
$parameters = array();
$out_types = array();
for ($i = 0; $i < mb_strlen($format); $i++) {
$char = mb_substr($format, $i, 1);
if (isset($patterns[$char])) {
$out_types[] = $char;
$parameters[] = $patterns[$char][0];
} else {
$parameters[] = $char;
}
}
if (!preg_match('/^'.implode($parameters).'$/u', $str, $out)) {
throw new RuntimeException(sprintf('Не удалось распарсить дату "%s" по шаблону "%s".', $str, $format));
}
// FIX
/* $date['year'] = (int)date('Y');
$date['month'] = (int)date('n');
$date['day'] = (int)date('j');
$date['hour'] = (int)date('h');
$date['minute'] = (int)date('i');
$date['second'] = (int)date('s');*/
$date['year'] = 1970;
$date['month'] = 1;
$date['day'] = 1;
$date['hour'] = 0;
$date['minute'] = 0;
$date['second'] = 0;
for ($i = 0; $i < count($out_types); $i++) {
$pattern = $patterns[$out_types[$i]];
if (isset($pattern[1])) {
$date[$pattern[1]] = (int)$out[$i + 1];
} else {
if ($out_types[$i] === 'U') {
$standard_date = getdate($out[$i + 1]);
$date['year'] = $standard_date['year'];
$date['month'] = $standard_date['mon'];
$date['day'] = $standard_date['mday'];
$date['hour'] = $standard_date['hours'];
$date['minute'] = $standard_date['minutes'];
$date['second'] = $standard_date['seconds'];
}
if ($out_types[$i] === 'y') {
$date['year'] = $out[$i + 1] + ($out[$i + 1] < 30 ? 2000 : 1900);
}
}
}
return $date;
}
function Date_createDate($format, $date) {
$patterns['Y'] = int_pad($date['year'], 4);
$patterns['y'] = int_pad($date['year'] - (int)($date['year'] / 100) * 100);
$patterns['m'] = int_pad($date['month']);
$patterns['n'] = $date['month'];
$patterns['d'] = int_pad($date['day']);
$patterns['j'] = $date['day'];
$patterns['H'] = int_pad($date['hour']);
$patterns['h'] = $date['hour'];
$patterns['G'] = int_pad($date['hour']);
$patterns['g'] = $date['hour'];
$patterns['i'] = int_pad($date['minute']);
$patterns['M'] = int_pad($date['minute']);
$patterns['s'] = int_pad($date['second']);
$patterns['U'] = mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']);
$parameters = array();
for ($i = 0; $i < mb_strlen($format); $i++) {
$char = mb_substr($format, $i, 1);
$parameters[] = isset($patterns[$char]) ? $patterns[$char] : $char;
}
$text = implode($parameters);
$replace['/Месяца/eu'] = 'ru_get_month_name('.(int)$date['month'].', 2)';
$replace['/Месяц/eu'] = 'ru_get_month_name('.(int)$date['month'].')';
$replace['/месяца/eu'] = 'mb_strtolower(ru_get_month_name('.(int)$date['month'].', 2))';
$replace['/месяц/eu'] = 'mb_strtolower(ru_get_month_name('.(int)$date['month'].'))';
$replace['/F/eu'] = 'en_get_month_name('.(int)$date['month'].')';
$replace['/f/eu'] = 'mb_strtolower(en_get_month_name('.(int)$date['month'].'))';
if ($patterns['U']) {
$replace['/Деньнедели/eu'] = 'ru_get_weekday_name(date("w", '.(int)$patterns['U'].'))';
$replace['/Деньнед/eu'] = 'ru_get_weekday_name(date("w", '.(int)$patterns['U'].'), true)';
$replace['/деньнедели/eu'] = 'mb_strtolower(ru_get_weekday_name(date("w", '.(int)$patterns['U'].')))';
$replace['/деньнед/eu'] = 'mb_strtolower(ru_get_weekday_name(date("w", '.(int)$patterns['U'].'), true))';
}
return preg_replace(array_keys($replace), $replace, $text);
}
function Date_checkDate($format, $str) {
$patterns['Y'] = array('(\d{4})', 'year');
$patterns['y'] = array('(\d{2})');
$patterns['m'] = array('(\d{2})', 'month');
$patterns['n'] = array('(\d{1,2})', 'month');
$patterns['d'] = array('(\d{2})', 'day');
$patterns['j'] = array('(\d{1,2})', 'day');
$patterns['H'] = array('(\d{2})', 'hour');
$patterns['h'] = array('(\d{1,2})', 'hour');
$patterns['i'] = array('(\d{1,2})', 'minute');
$patterns['M'] = array('(\d{2})', 'minute');
$patterns['s'] = array('(\d{1,2})', 'second');
$patterns['U'] = array('(-?\d+)');
$parameters = array();
$out_types = array();
for ($i = 0; $i < mb_strlen($format); $i++) {
$char = mb_substr($format, $i, 1);
if (isset($patterns[$char])) {
$out_types[] = $char;
$parameters[] = $patterns[$char][0];
} else {
$parameters[] = $char;
}
}
return (bool)preg_match('/^'.implode($parameters).'$/u', $str, $out);
}
function Date_convert($format_from, $format_to, $str) {
$date = Date_parseDate($format_from, $str);
return Date_createDate($format_to, $date);
}
$t = microtime(true);
for ($i = 0; $i < 2000; $i++) {
Date_convert('Y-m-d', 'd.m.Y H:i:s', '2000-01-01');
}
echo 'DateConvert: '.(microtime(true) - $t)."\n";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.3.1 0.007 0.033 16.87 7.3.0 0.009 0.038 16.68 7.2.13 0.013 0.043 17.25 7.2.12 0.006 0.039 17.11 7.2.11 0.014 0.035 17.17 7.2.10 0.023 0.024 17.27 7.2.9 0.007 0.038 16.86 7.2.8 0.013 0.034 17.34 7.2.7 0.020 0.037 17.29 7.2.6 0.027 0.034 17.39 7.2.5 0.019 0.034 16.95 7.2.4 0.010 0.041 16.84 7.2.3 0.011 0.032 17.02 7.2.2 0.010 0.045 17.31 7.2.1 0.017 0.029 17.06 7.2.0 0.010 0.027 18.52 7.1.25 0.015 0.033 16.12 7.1.7 0.003 0.019 17.39 7.1.6 0.007 0.038 19.24 7.1.5 0.014 0.037 17.55 7.1.0 0.000 0.097 22.41 7.0.20 0.000 0.030 17.15 7.0.14 0.013 0.093 22.03 7.0.10 0.013 0.123 20.07 7.0.9 0.017 0.100 19.98 7.0.8 0.010 0.103 20.27 7.0.7 0.007 0.063 20.08 7.0.6 0.003 0.107 20.05 7.0.5 0.010 0.093 20.45 7.0.4 0.007 0.070 20.07 7.0.3 0.003 0.103 20.00 7.0.2 0.013 0.100 20.07 7.0.1 0.013 0.107 19.92 7.0.0 0.003 0.110 19.96 5.6.28 0.003 0.110 20.88 5.6.25 0.010 0.100 20.82 5.6.24 0.000 0.127 20.63 5.6.23 0.000 0.110 20.61 5.6.22 0.007 0.100 20.69 5.6.21 0.020 0.090 20.66 5.6.20 0.007 0.110 21.09 5.6.19 0.003 0.097 21.16 5.6.18 0.003 0.107 21.13 5.6.17 0.017 0.087 21.01 5.6.16 0.010 0.097 21.02 5.6.15 0.013 0.100 21.07 5.6.14 0.013 0.103 21.09 5.6.13 0.010 0.097 21.19 5.6.12 0.007 0.107 21.08 5.6.11 0.017 0.100 21.21 5.6.10 0.017 0.100 21.08 5.6.9 0.007 0.113 21.20 5.6.8 0.003 0.103 20.60 5.6.7 0.017 0.077 20.36 5.6.6 0.010 0.080 20.42 5.6.5 0.007 0.100 20.52 5.6.4 0.007 0.107 20.54 5.6.3 0.010 0.097 20.55 5.6.2 0.003 0.083 20.54 5.6.1 0.003 0.083 20.54 5.6.0 0.007 0.063 20.45 5.5.38 0.010 0.103 20.51 5.5.37 0.020 0.090 20.45 5.5.36 0.007 0.103 20.54 5.5.35 0.023 0.093 20.43 5.5.34 0.003 0.097 20.96 5.5.33 0.013 0.100 20.85 5.5.32 0.003 0.107 20.86 5.5.31 0.007 0.057 20.93 5.5.30 0.010 0.103 20.91 5.5.29 0.010 0.110 20.78 5.5.28 0.023 0.090 20.85 5.5.27 0.007 0.107 20.85 5.5.26 0.007 0.110 20.88 5.5.25 0.010 0.103 20.64 5.5.24 0.017 0.097 20.22 5.5.23 0.007 0.103 20.23 5.5.22 0.013 0.090 20.07 5.5.21 0.010 0.090 20.21 5.5.20 0.023 0.090 20.31 5.5.19 0.007 0.093 20.18 5.5.18 0.000 0.060 20.29 5.5.16 0.007 0.053 20.32 5.5.15 0.010 0.050 20.29 5.5.14 0.023 0.043 20.25 5.5.13 0.007 0.070 20.16 5.5.12 0.003 0.060 20.24 5.5.11 0.007 0.053 20.04 5.5.10 0.000 0.060 20.12 5.5.9 0.000 0.063 20.12 5.5.8 0.003 0.060 20.05 5.5.7 0.000 0.060 20.13 5.5.6 0.000 0.060 20.08 5.5.5 0.003 0.057 20.15 5.5.4 0.007 0.053 20.11 5.5.3 0.007 0.100 20.20 5.5.2 0.003 0.057 20.10 5.5.1 0.000 0.060 20.18 5.5.0 0.003 0.117 20.18 5.4.45 0.010 0.153 19.39 5.4.44 0.003 0.167 19.34 5.4.43 0.007 0.163 19.49 5.4.42 0.003 0.180 19.47 5.4.41 0.017 0.170 19.26 5.4.40 0.003 0.183 19.17 5.4.39 0.003 0.170 19.24 5.4.38 0.010 0.160 19.13 5.4.37 0.017 0.137 19.13 5.4.36 0.013 0.147 18.92 5.4.35 0.037 0.103 18.93 5.4.34 0.003 0.110 19.14 5.4.32 0.007 0.120 18.92 5.4.31 0.003 0.117 19.04 5.4.30 0.007 0.117 19.13 5.4.29 0.010 0.137 18.92 5.4.28 0.007 0.113 18.98 5.4.27 0.007 0.133 18.96 5.4.26 0.003 0.123 19.24 5.4.25 0.010 0.137 19.14 5.4.24 0.007 0.110 19.23 5.4.23 0.013 0.103 18.97 5.4.22 0.013 0.097 19.05 5.4.21 0.003 0.160 18.85 5.4.20 0.003 0.117 19.12 5.4.19 0.013 0.097 19.08 5.4.18 0.010 0.107 19.09 5.4.17 0.007 0.150 19.14 5.4.16 0.010 0.160 19.04 5.4.15 0.013 0.163 18.89 5.4.14 0.007 0.150 16.48 5.4.13 0.013 0.153 16.48 5.4.12 0.007 0.150 16.34 5.4.11 0.003 0.167 16.53 5.4.10 0.007 0.150 16.54 5.4.9 0.003 0.153 16.55 5.4.8 0.000 0.173 16.45 5.4.7 0.007 0.173 16.52 5.4.6 0.010 0.147 16.40 5.4.5 0.003 0.163 16.43 5.4.4 0.003 0.123 16.52 5.4.3 0.007 0.143 16.54 5.4.2 0.000 0.163 16.32 5.4.1 0.007 0.157 16.43 5.4.0 0.010 0.147 15.93 5.3.29 0.010 0.157 14.75 5.3.28 0.003 0.157 14.86 5.3.27 0.000 0.177 14.69 5.3.26 0.010 0.160 14.86 5.3.25 0.007 0.190 14.85 5.3.24 0.013 0.180 14.79 5.3.23 0.007 0.170 14.66 5.3.22 0.007 0.157 14.82 5.3.21 0.000 0.200 14.76 5.3.20 0.003 0.170 14.84 5.3.19 0.007 0.170 14.71 5.3.18 0.003 0.163 14.68 5.3.17 0.007 0.170 14.76 5.3.16 0.000 0.200 14.62 5.3.15 0.007 0.177 14.68 5.3.14 0.017 0.163 14.69 5.3.13 0.013 0.183 14.75 5.3.12 0.007 0.167 14.74 5.3.11 0.010 0.167 14.70 5.3.10 0.003 0.170 14.18 5.3.9 0.010 0.167 14.13 5.3.8 0.007 0.173 14.16 5.3.7 0.010 0.173 14.21 5.3.6 0.007 0.163 14.24 5.3.5 0.007 0.170 14.16 5.3.4 0.007 0.193 14.18 5.3.3 0.010 0.190 14.12 5.3.2 0.000 0.180 13.71 5.3.1 0.007 0.177 13.92 5.3.0 0.013 0.170 19.36 5.2.17 0.007 0.057 11.91 5.2.16 0.010 0.057 11.91 5.2.15 0.010 0.050 11.91 5.2.14 0.000 0.070 11.91 5.2.13 0.010 0.057 11.91 5.2.12 0.010 0.060 11.91 5.2.11 0.013 0.057 11.91 5.2.10 0.010 0.053 11.91 5.2.9 0.003 0.063 11.91 5.2.8 0.007 0.060 11.91 5.2.7 0.013 0.053 11.91 5.2.6 0.007 0.060 11.91 5.2.5 0.007 0.063 11.91 5.2.4 0.007 0.037 11.91 5.2.3 0.003 0.057 11.91 5.2.2 0.007 0.057 11.91 5.2.1 0.003 0.060 11.91 5.2.0 0.007 0.050 11.91 5.1.6 0.010 0.043 11.91 5.1.5 0.007 0.053 11.91 5.1.4 0.003 0.050 11.91 5.1.3 0.003 0.040 11.91 5.1.2 0.007 0.057 11.91 5.1.1 0.017 0.030 11.91 5.1.0 0.003 0.053 11.91 5.0.5 0.000 0.047 11.91 5.0.4 0.003 0.023 11.91 5.0.3 0.003 0.063 11.91 5.0.2 0.003 0.043 11.91 5.0.1 0.000 0.047 11.91 5.0.0 0.000 0.063 11.91 4.4.9 0.007 0.033 11.91 4.4.8 0.000 0.037 11.91 4.4.7 0.003 0.033 11.91 4.4.6 0.000 0.023 11.91 4.4.5 0.003 0.033 11.91 4.4.4 0.003 0.050 11.91 4.4.3 0.003 0.033 11.91 4.4.2 0.003 0.033 11.91 4.4.1 0.003 0.037 11.91 4.4.0 0.000 0.057 11.91 4.3.11 0.000 0.030 11.91 4.3.10 0.007 0.030 11.91 4.3.9 0.000 0.037 11.91 4.3.8 0.000 0.057 11.91 4.3.7 0.000 0.037 11.91 4.3.6 0.000 0.037 11.91 4.3.5 0.003 0.033 11.91 4.3.4 0.000 0.053 11.91 4.3.3 0.003 0.033 11.91 4.3.2 0.000 0.037 11.91 4.3.1 0.000 0.037 11.91 4.3.0 0.000 0.030 11.91
preferences:dark mode live preview
37.1 ms | 401 KiB | 5 Q