|  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
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: sparky89 at gmx dot de
New email:
PHP Version: OS:


 [2009-07-07 12:41 UTC] sparky89 at gmx dot de
as of 5.2 there are features of DateTime class.

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:
  echo '<pre>';
  $date1 = new DateTime('2009-07-07 13:20:12');
  setlocale(LC_ALL, 'German');
  var_dump(strftime('%A', $date1->format('U')));
  setlocale(LC_ALL, 'English');
  var_dump(strftime('%A', $date1->getTimestamp()));
  echo '</pre>';

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"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-08 08:26 UTC]
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]
-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:

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]
-Status: Assigned +Status: Closed
 [2017-01-11 16:31 UTC]
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(
echo $formatter->format($date);

I'm therefore closing this as fixed.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Jun 02 08:03:37 2023 UTC