I hope this will save some time for others who have to struggle with different php versions on dev and prod plattform in the future:
when formatting a DateTime object with a *custom pattern*, be sure to use a timestamp to pass at the IntlDateFormatter::format in order to have it working on different php versions:
Example for PHP Version 5.3.5-1ubuntu7.2 (my dev machine):
<?php
$date = new \DateTime();
$dateFormatter = \IntlDateFormatter::create(
\Locale::getDefault(),
\IntlDateFormatter::NONE,
\IntlDateFormatter::NONE,
\date_default_timezone_get(),
\IntlDateFormatter::GREGORIAN,
'EEEE'
);
var_dump($dateFormatter->format($date)); // string(6) "Monday"
?>
Example for PHP Version 5.3.2-1ubuntu4.9 (the prod server):
<?php
// same formatting as above
var_dump($dateFormatter->format($date)); // bool(false)
?>
When using $dateFormatter->format($date->getTimestamp()), you'll always get the formatted and localized string instead of a false.
IntlDateFormatter::format
datefmt_format
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
IntlDateFormatter::format -- datefmt_format — Tarih/saat değerini bir dizge olarak biçemler
Açıklama
Nesne yönelimli kullanım
Yordamsal kullanım
Zaman değerini bir dizge olarak biçemler.
Değiştirgeler
-
biçemleyici -
Biçemleyici nesne.
-
değer -
Biçemlenecek değer. Tamsayı olarak bir Unix zaman damgası (Unix zaman başlangıcından itibaren saniye sayısı) olabileceği gibi bir localtime() dizisi de olabilir.
Dönen Değerler
Bir hata oluşmuşsa FALSE aksi takdirde biçemli dizge.
Örnekler
Örnek 1 - datefmt_format() örneği
<?php
$fmt = datefmt_create("tr_TR", IntlDateFormatter::FULL,
IntlDateFormatter::FULL,'Europe/Istanbul',
IntlDateFormatter::GREGORIAN);
echo "İlk biçemli çıktı: ".datefmt_format($fmt, 1234567890);
$fmt = datefmt_create("en-US", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Europe/Istanbul',
IntlDateFormatter::GREGORIAN);
echo "\nİkinci biçemli çıktı: ".datefmt_format($fmt, 1234567890);
$fmt = datefmt_create("tr_TR", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Europe/Istanbul',
IntlDateFormatter::GREGORIAN, "dd/MM/yyyy");
echo "\nİlk kalıbın çıktısı: ".datefmt_format( $fmt, 1234567890);
$fmt = datefmt_create("en-US", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Europe/Istanbul',
IntlDateFormatter::GREGORIAN, "MM/dd/yyyy");
echo "\nİkinci kalıbın çıktısı: ".datefmt_format($fmt, 1234567890);
?>
Örnek 2 - Nesne yönelimli kullanım örneği
<?php
$fmt = new IntlDateFormatter("tr_TR", IntlDateFormatter::FULL,
IntlDateFormatter::FULL,'Europe/Istanbul',
IntlDateFormatter::GREGORIAN);
echo "İlk biçemli çıktı: ".$fmt->format(1234567890);
$fmt = new IntlDateFormatter("en-US", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Europe/Istanbul',
IntlDateFormatter::GREGORIAN);
echo "\nİkinci biçemli çıktı: ".$fmt->format(1234567890);
$fmt = new IntlDateFormatter("tr_TR", IntlDateFormatter::FULL,
IntlDateFormatter::FULL,'Europe/Istanbul',
IntlDateFormatter::GREGORIAN, "dd/MM/yyyy");
echo "\nİlk kalıbın çıktısı: ".$fmt->format(1234567890);
$fmt = new IntlDateFormatter("en-US", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Europe/Istanbul',
IntlDateFormatter::GREGORIAN, "MM/dd/yyyy");
echo "\nİkinci kalıbın çıktısı: ".$fmt->format(1234567890);
?>
Yukarıdaki örneğin çıktısı:
İlk biçemli çıktı: 14 Şubat 2009 Cumartesi 01:31:30 Türkiye Saati İkinci biçemli çıktı: Saturday, February 14, 2009 1:31:30 AM Turkey Time İlk kalıbın çıktısı: 14/02/2009 İkinci kalıbın çıktısı: 02/14/2009
Ayrıca Bakınız
- datefmt_create() - Bir tarih biçemleyici oluşturur
- datefmt_parse() - Dizgeyi bir zaman damgası değerine dönüştürür
- datefmt_get_error_code() - Son işlemin hata kodunu döndürür
- datefmt_get_error_message() - Son işlemin hata iletisini döndürür
con at bartrail dot de ¶
1 year ago
mail dot dennisbecker at gmail dot com ¶
4 months ago
You should know that PHPs IntlDateFormatter class uses ISO date format codes instead of PHPs date() format codes. It is not really clear mentioned from my point of view.
A good list to find ISO codes is available at http://framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefinedformats and such a list should be added here, too.
alex ¶
3 years ago
It's important to note that a conversion of timezones will be made from the default timezone (date_default_timezone_set()) and the timezone you passed in the constructor (or datefmt_create()).
If you are inserting the dates into your database as UTC time, make sure to set date_default_timezone_set to UTC as well (or any other timezone, but they need to be the same). Once you call ::format, you will get the converted time.
