Edit report at https://bugs.php.net/bug.php?id=48834&edit=1
ID: 48834
Comment by: ale at alejandrolapeyre dot com dot ar
Reported by: sparky89 at gmx dot de
Summary: DateTime should also use locales
Status: Assigned
Type: Feature/Change Request
Package: Date/time related
Operating System: Debian
PHP Version: 5.2.10
Assigned To: derick
Block user comment: N
Private report: N
New Comment:
date_format / DateTime::format can't be changed to use locales, because that
would break a lot of programs.
A new function date_format_locale / DateTime::formatLocale would be an
excellent addition.
I use this as a workaround now:
<?php
function date_format_locale(DateTime $dt, $format)
{
$len = strlen($format);
if (strcspn($format, 'DlFM') >= $len) {
return $dt->format($format);
}
$marker = "\x0B";
$keys = array('D' => '%a', 'l' => '%A', 'F' => '%B', 'M' => '%b');
$start = 0;
while (($start += strcspn($format, 'DlFM\\', $start)) < $len) {
$char = $format[$start];
if ('\\' === $char) {
$start += 2;
} else {
$format[$start++] = $marker;
$replacements[] = $keys[$char];
}
}
if (!isset($replacements)) {
return $dt->format($format);
}
list($wday, $month) = explode(' ', $dt->format('w n'));
$wday = (int) $wday;
$month = (int) $month;
$sundays = array(0, 2, 6, 6, 3, 1, 5, 3, 7, 4, 2, 6, 4);
$day = $sundays[$month] + $wday;
$ts = gmmktime($hour=0, $minute=0, $second=0, $month, $day, $year=2011);
$replacements = explode($marker, gmstrftime(implode($marker,
$replacements), $ts));
$ret = $dt->format($format);
while (false !== $i = strpos($ret, $marker)) {
$str = array_shift($replacements);
//$str = mb_convert_case($str, MB_CASE_TITLE);
$ret = substr_replace($ret, $str, $i, 1);
}
return $ret;
}
?>
Hope this helps, best regards!
Previous Comments:
------------------------------------------------------------------------
[2009-07-08 08:26:29] [email protected]
This is indeed planned for PHP 6, where there is an experimental function
"date_format_locale()" alreadyâbut that is bound to change.
------------------------------------------------------------------------
[2009-07-07 12:41:59] sparky89 at gmx dot de
Description:
------------
as of 5.2 there are features of DateTime class.
http://de.php.net/datetime
Im asking myself at the moment, why it does not look up locales. It is somehow
stupid to use datetime, convert it to timestamp just to get an output of the
month for any language, assuming strftime to convert the timestamp.
I hope this will be changed.
Reproduce code:
---------------
<?php
echo '<pre>';
$date1 = new DateTime('2009-07-07 13:20:12');
setlocale(LC_ALL, 'German');
var_dump(strftime('%A', $date1->format('U')));
var_dump($date1->format('l'));
setlocale(LC_ALL, 'English');
var_dump(strftime('%A', $date1->getTimestamp()));
var_dump($date1->format('l'));
echo '</pre>';
highlight_file(__FILE__);
?>
Expected result:
----------------
string(8) "Dienstag"
string(7) "Dienstag"
string(7) "Tuesday"
string(7) "Tuesday"
Actual result:
--------------
string(8) "Dienstag"
string(7) "Tuesday"
string(7) "Tuesday"
string(7) "Tuesday"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=48834&edit=1