<?php
interface PatternModifier {
public function replace($pattern);
}
class RegexRemove implements PatternModifier {
private $stripchars;
public function __construct($stripchars) {
$this->stripchars = $stripchars;
}
public function replace($pattern) {
return preg_replace('/[' . $this->stripchars . ']/', '', $pattern);
}
}
class StartEndTrim implements PatternModifier {
private $skipstart;
private $skipend;
public function __construct($skipstart, $skipend) {
$this->skipstart = $skipstart;
$this->skipend = $skipend;
}
public function replace($pattern) {
return substr($pattern, $this->skipstart, -$this->skipend);
}
}
class RegexReplace implements PatternModifier {
private $search;
private $replace;
public function __construct($search, $replace) {
$this->search = $search;
$this->replace = $replace;
}
public function replace($pattern) {
return preg_replace('/' . $this->search . '/', $this->replace, $pattern);
}
}
class Combined implements PatternModifier {
private $modifiers;
public function __construct($modifiers) {
$this->modifiers = $modifiers;
}
public function replace($pattern) {
$result = $pattern;
foreach($this->modifiers as $m) {
$result = $m->replace($result);
}
return $result;
}
}
function mik_date($locale, $t, $format, $pattern_modifier = null) {
$idf = new IntlDateFormatter($locale, $format, IntlDateFormatter::NONE);
if($pattern_modifier != null) {
$pattern = $idf->getPattern();
$pattern = $pattern_modifier->replace($pattern);
$idf->setPattern($pattern);
}
return $idf->format($t);
}
function mik_time($locale, $t, $format, $pattern_modifier = null) {
$idf = new IntlDateFormatter($locale, IntlDateFormatter::NONE, $format);
if($pattern_modifier != null) {
$pattern = $idf->getPattern();
$pattern = $pattern_modifier->replace($pattern);
$idf->setPattern($pattern);
}
return $idf->format($t);
}
function mik_datetime($locale, $t, $date_format, $time_format, $delim = ' ', $date_pattern_modifier = null, $time_pattern_modifier = null) {
return mik_date($locale, $t, $date_format, $date_pattern_modifier) . $delim . mik_time($locale, $t, $time_format, $time_pattern_modifier);
}
$t = time();
echo mik_datetime('da_DK', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM) . "\r\n";
echo mik_datetime('en_US', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM) . "\r\n";
echo mik_datetime('da_DK', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM, ' ', new Combined([new RegexRemove('y'), new StartEndTrim(0, 1)]), new Combined([new RegexReplace('mm[:\.]ss', 'mm')])) . "\r\n";
echo mik_datetime('en_US', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM, ' ', new Combined([new RegexRemove('y'), new StartEndTrim(0, 1)]), new Combined([new RegexReplace('mm[:\.]ss', 'mm')])) . "\r\n";
?>
<?php
function mik_date($locale, $t, $format, $stripchars = null, $skipstart = 0, $skipend = -100) {
$idf = new IntlDateFormatter($locale, $format, IntlDateFormatter::NONE);
if($stripchars != null) {
$pattern = $idf->getPattern();
$pattern = substr(preg_replace('/[' . $stripchars . ']/', '', $pattern), $skipstart, -$skipend);
$idf->setPattern($pattern);
}
return $idf->format($t);
}
function mik_time($locale, $t, $format, $search = null, $replace = null) {
$idf = new IntlDateFormatter($locale, IntlDateFormatter::NONE, $format);
if($search != null) {
$pattern = $idf->getPattern();
$pattern = preg_replace('/' . $search . '/', $replace, $pattern);
$idf->setPattern($pattern);
}
return $idf->format($t);
}
function mik_datetime($locale, $t, $dateformat, $timeformat, $delim = ' ', $stripchars = null, $skipstart = 0, $skipend = -100, $search = null, $replace = null) {
return mik_date($locale, $t, $dateformat, $stripchars, $skipstart, $skipend) . $delim . mik_time($locale, $t, $timeformat, $search, $replace);
}
$t = time();
echo mik_datetime('da_DK', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM) . "\r\n";
echo mik_datetime('en_US', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM) . "\r\n";
echo mik_datetime('da_DK', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM, ' ', 'y', 0, 1, 'mm[:\.]ss', 'mm') . "\r\n";
echo mik_datetime('en_US', $t, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM, ' ', 'y', 0, 1, 'mm[:\.]ss', 'mm') . "\r\n";
?>