@ 2013-11-01T07:47:24Z <?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 < 10000; $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 < 10000; $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.12 0.013 0.093 15.30 7.3.11 0.007 0.089 15.39 7.3.10 0.003 0.091 15.55 7.3.9 0.007 0.085 15.39 7.3.8 0.003 0.113 15.37 7.3.7 0.010 0.090 15.49 7.3.6 0.003 0.091 15.29 7.3.5 0.007 0.091 15.28 7.3.4 0.003 0.097 15.18 7.3.3 0.000 0.096 15.34 7.3.2 0.013 0.091 17.12 7.3.1 0.013 0.085 17.12 7.3.0 0.010 0.083 17.11 7.2.24 0.013 0.080 15.35 7.2.23 0.003 0.095 15.27 7.2.22 0.007 0.107 15.77 7.2.21 0.010 0.082 15.38 7.2.20 0.003 0.085 15.36 7.2.19 0.007 0.100 15.25 7.2.18 0.000 0.101 15.51 7.2.17 0.010 0.085 15.38 7.2.16 0.013 0.098 15.71 7.2.15 0.010 0.082 17.24 7.2.14 0.003 0.099 17.12 7.2.13 0.003 0.087 17.21 7.2.12 0.003 0.125 17.14 7.2.11 0.000 0.092 17.24 7.2.10 0.003 0.098 17.33 7.2.9 0.003 0.090 17.34 7.2.8 0.000 0.091 17.20 7.2.7 0.010 0.082 17.08 7.2.6 0.003 0.093 17.09 7.2.5 0.007 0.081 17.28 7.2.4 0.003 0.094 17.15 7.2.3 0.013 0.082 17.04 7.2.2 0.007 0.086 17.34 7.2.1 0.010 0.090 17.38 7.2.0 0.007 0.085 18.37 7.1.33 0.007 0.090 16.16 7.1.32 0.017 0.089 15.93 7.1.31 0.010 0.099 16.01 7.1.30 0.007 0.092 16.07 7.1.29 0.007 0.095 16.20 7.1.28 0.003 0.098 15.81 7.1.27 0.003 0.099 15.99 7.1.26 0.000 0.110 16.00 7.1.25 0.010 0.096 15.94 7.1.7 0.000 0.066 17.51 7.1.6 0.016 0.108 19.24 7.1.5 0.010 0.109 17.60 7.1.0 0.000 0.097 22.39 7.0.20 0.003 0.070 17.26 7.0.14 0.000 0.177 22.18 7.0.8 0.070 0.127 20.16 7.0.7 0.043 0.137 20.06 7.0.6 0.073 0.133 20.13 7.0.5 0.043 0.150 20.43 7.0.4 0.017 0.150 20.02 7.0.3 0.010 0.153 19.97 7.0.2 0.013 0.163 20.14 7.0.1 0.017 0.160 20.06 7.0.0 0.010 0.147 19.97 5.6.28 0.007 0.213 21.02 5.6.23 0.003 0.133 20.73 5.6.22 0.010 0.187 20.79 5.6.21 0.003 0.183 20.68 5.6.20 0.020 0.127 21.24 5.6.19 0.010 0.150 21.17 5.6.18 0.010 0.193 21.19 5.6.17 0.023 0.177 21.16 5.6.16 0.013 0.173 21.23 5.6.15 0.010 0.173 21.06 5.6.14 0.013 0.187 21.21 5.6.13 0.013 0.180 21.14 5.6.12 0.007 0.153 21.21 5.6.11 0.020 0.157 21.11 5.6.10 0.007 0.167 21.14 5.6.9 0.007 0.127 21.16 5.6.8 0.007 0.193 20.66 5.6.7 0.007 0.133 20.56 5.6.6 0.010 0.173 20.48 5.6.5 0.007 0.140 20.55 5.6.4 0.003 0.150 20.48 5.6.3 0.007 0.183 20.52 5.6.2 0.013 0.153 20.46 5.6.1 0.000 0.180 20.46 5.6.0 0.003 0.210 20.48 5.5.37 0.010 0.227 20.53 5.5.36 0.000 0.163 20.45 5.5.35 0.023 0.180 20.53 5.5.34 0.003 0.240 20.90 5.5.33 0.007 0.223 21.05 5.5.32 0.007 0.273 20.95 5.5.31 0.017 0.247 20.88 5.5.30 0.010 0.240 20.89 5.5.29 0.010 0.197 20.87 5.5.28 0.003 0.150 20.98 5.5.27 0.010 0.163 21.00 5.5.26 0.010 0.170 21.00 5.5.25 0.007 0.233 20.81 5.5.24 0.007 0.170 20.38 5.5.23 0.010 0.233 20.36 5.5.22 0.013 0.270 20.39 5.5.21 0.020 0.223 20.41 5.5.20 0.027 0.227 20.39 5.5.19 0.007 0.170 20.34 5.5.18 0.000 0.180 20.29 5.5.16 0.007 0.270 20.39 5.5.15 0.013 0.167 20.36 5.5.14 0.010 0.323 20.38 5.5.13 0.003 0.227 20.34 5.5.12 0.010 0.120 20.33 5.5.11 0.023 0.230 20.23 5.5.10 0.003 0.233 20.25 5.5.9 0.013 0.220 20.23 5.5.8 0.013 0.170 20.16 5.5.7 0.013 0.157 20.16 5.5.6 0.003 0.220 20.18 5.5.5 0.010 0.213 20.27 5.5.4 0.003 0.157 20.18 5.5.3 0.000 0.123 20.13 5.5.2 0.017 0.210 20.18 5.5.1 0.003 0.133 20.14 5.5.0 0.000 0.100 20.21 5.4.45 0.003 0.467 19.39 5.4.44 0.017 0.510 19.21 5.4.43 0.007 0.423 19.30 5.4.42 0.010 0.487 19.55 5.4.41 0.007 0.453 19.13 5.4.40 0.010 0.427 19.08 5.4.39 0.007 0.447 19.07 5.4.38 0.003 0.433 19.13 5.4.37 0.013 0.430 18.88 5.4.36 0.007 0.437 19.21 5.4.35 0.000 0.427 19.07 5.4.34 0.003 0.380 19.25 5.4.32 0.013 0.413 19.23 5.4.31 0.000 0.393 19.14 5.4.30 0.010 0.440 19.23 5.4.29 0.013 0.453 19.14 5.4.28 0.007 0.423 19.14 5.4.27 0.017 0.413 19.07 5.4.26 0.007 0.417 19.08 5.4.25 0.020 0.460 19.09 5.4.24 0.003 0.423 19.04 5.4.23 0.007 0.453 19.20 5.4.22 0.013 0.497 18.91 5.4.21 0.003 0.460 19.03 5.4.20 0.003 0.383 19.05 5.4.19 0.007 0.330 18.91 5.4.18 0.003 0.310 19.05 5.4.17 0.003 0.317 18.96 5.4.16 0.010 0.333 19.02 5.4.15 0.003 0.400 19.05 5.4.14 0.007 0.350 18.13 5.4.13 0.003 0.303 18.13 5.4.12 0.007 0.390 18.13 5.4.11 0.013 0.447 18.13 5.4.10 0.010 0.433 18.13 5.4.9 0.013 0.400 18.13 5.4.8 0.003 0.437 18.13 5.4.7 0.003 0.350 18.13 5.4.6 0.007 0.323 18.13 5.4.5 0.007 0.443 18.13 5.4.4 0.000 0.393 18.13 5.4.3 0.010 0.407 18.13 5.4.2 0.007 0.433 18.13 5.4.1 0.003 0.473 18.13 5.4.0 0.017 0.440 18.13 5.3.29 0.003 0.460 18.13 5.3.28 0.003 0.500 18.13 5.3.27 0.000 0.420 18.13 5.3.26 0.007 0.363 18.13 5.3.25 0.007 0.377 18.13 5.3.24 0.003 0.357 18.13 5.3.23 0.003 0.480 18.13 5.3.22 0.003 0.473 18.13 5.3.21 0.013 0.487 18.13 5.3.20 0.003 0.493 18.13 5.3.19 0.007 0.473 18.13 5.3.18 0.013 0.540 18.13 5.3.17 0.010 0.627 18.13 5.3.16 0.003 0.500 18.13 5.3.15 0.003 0.500 18.13 5.3.14 0.007 0.497 18.13 5.3.13 0.010 0.447 18.13 5.3.12 0.013 0.480 18.13 5.3.11 0.007 0.513 18.13 5.3.10 0.007 0.510 18.13 5.3.9 0.003 0.490 18.13 5.3.8 0.010 0.520 18.13 5.3.7 0.007 0.597 18.13 5.3.6 0.073 0.503 18.13 5.3.5 0.003 0.513 18.13 5.3.4 0.007 0.507 18.13 5.3.3 0.007 0.517 18.13 5.3.2 0.000 0.500 18.13 5.3.1 0.003 0.453 18.13 5.3.0 0.017 0.517 41.18 5.2.17 0.013 0.040 18.13 5.2.16 0.013 0.027 18.13 5.2.15 0.007 0.057 18.13 5.2.14 0.003 0.033 18.13 5.2.13 0.003 0.043 18.13 5.2.12 0.007 0.037 18.13 5.2.11 0.003 0.063 18.13 5.2.10 0.000 0.033 18.13 5.2.9 0.003 0.063 18.13 5.2.8 0.003 0.063 18.13 5.2.7 0.003 0.050 18.13 5.2.6 0.007 0.060 18.13 5.2.5 0.007 0.060 18.13 5.2.4 0.013 0.047 18.13 5.2.3 0.003 0.043 18.13 5.2.2 0.003 0.050 18.13 5.2.1 0.007 0.053 18.13 5.2.0 0.003 0.037 18.13 5.1.6 0.007 0.050 18.13 5.1.5 0.003 0.057 18.13 5.1.4 0.007 0.050 18.13 5.1.3 0.000 0.047 18.13 5.1.2 0.010 0.033 18.13 5.1.1 0.000 0.030 18.13 5.1.0 0.007 0.053 18.13 5.0.5 0.003 0.047 18.13 5.0.4 0.003 0.047 18.13 5.0.3 0.020 0.043 18.13 5.0.2 0.003 0.043 18.13 5.0.1 0.003 0.043 18.13 5.0.0 0.003 0.050 18.13 4.4.9 0.000 0.030 18.13 4.4.8 0.007 0.020 18.13 4.4.7 0.000 0.040 18.13 4.4.6 0.000 0.037 18.13 4.4.5 0.000 0.037 18.13 4.4.4 0.003 0.053 18.13 4.4.3 0.007 0.033 18.13 4.4.2 0.000 0.017 18.13 4.4.1 0.003 0.033 18.13 4.4.0 0.003 0.057 18.13 4.3.11 0.000 0.037 18.13 4.3.10 0.003 0.033 18.13 4.3.9 0.007 0.020 18.13 4.3.8 0.003 0.030 18.13 4.3.7 0.000 0.030 18.13 4.3.6 0.003 0.033 18.13 4.3.5 0.007 0.030 18.13 4.3.4 0.003 0.040 18.13 4.3.3 0.000 0.023 18.13 4.3.2 0.003 0.013 18.13 4.3.1 0.000 0.037 18.13 4.3.0 0.010 0.027 18.13
preferences:dark mode live preview
39.19 ms | 400 KiB | 5 Q