php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48834 DateTime should also use locales
Submitted: 2009-07-07 12:41 UTC Modified: 2017-01-11 16:31 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:3 (100.0%)
From: sparky89 at gmx dot de Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.2.10 OS: Debian
Private report: No CVE-ID: None
 [2009-07-07 12:41 UTC] 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"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-08 08:26 UTC] derick@php.net
This is indeed planned for PHP 6, where there is an experimental function "date_format_locale()" already—but that is bound to change.
 [2010-12-20 14:29 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
 [2011-12-07 01:42 UTC] ale at alejandrolapeyre dot com dot ar
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!
 [2017-01-11 16:31 UTC] heiglandreas@php.net
-Status: Assigned +Status: Closed
 [2017-01-11 16:31 UTC] heiglandreas@php.net
This issue is by now more than 7 years old and targets an unsupported version of PHP.

Using a supported version of PHP you can do this via:

$date = new DateTime('2009-07-07 13:20:12');
$formatter = new IntlDateFormatter(
    'de-DE',
    IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL, 
    $date->getTimezone(), 
    IntlDateFormatter::GREGORIAN, 
    'EEEE'
);
echo $formatter->format($date);

I'm therefore closing this as fixed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC