I have a DateTime object which I'm currently formating via
$mytime->format("D d.m.Y")
Which gives me exactly the format I need:
Tue 5.3.2012
The only missing point is the correct language. I need German translation of Tue (Tuesday), which is Die (Dienstag).
This gives me the right locale setting
Locale::getDefault()
But I don't know how to tell DateTime::format to use it.
Isn't there a way to do something like:
$mytime->format("D d.m.Y", \Locale::getDefault());
You can use the Intl extension to format the date. It will format dates/times according to the chosen locale, or you can override that with IntlDateFormatter::setPattern().
A quicky example of using a custom pattern, for your desired output format, might look like.
$dt = new DateTime;
$formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
$formatter->setPattern('E d.M.yyyy');
echo $formatter->format($dt);
Which outputs the following (for today, at least).
Di. 4.6.2013
That's because format does not pay attention to locale. You should use strftime instead.
For example:
setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp())
IntlDateFormatter is the way to go currently (2023).
<?php
$formatter = new IntlDateFormatter(
$locale, // the locale to use, e.g. 'en_GB'
$dateFormat, // how the date should be formatted, e.g. IntlDateFormatter::FULL
$timeFormat, // how the time should be formatted, e.g. IntlDateFormatter::FULL
'Europe/Berlin' // the time should be returned in which timezone?
);
echo $formatter->format(time());
Will give a different output, depending on what you pass as $locale and the date and time format. I wanted to add some samples for future reference. Note that IntlDateFormatter::GREGORIAN and IntlDateFormatter::LONG are interchangable.
Locale: en_US
Format for Date & Time: Results in:
IntlDateFormatter::FULL Friday, August 5, 2022 at 3:26:37 PM Central European Summer Time
IntlDateFormatter::LONG August 5, 2022 at 3:26:37 PM GMT+2
IntlDateFormatter::MEDIUM Aug 5, 2022, 3:26:37 PM
IntlDateFormatter::SHORT 8/5/22, 3:26 PM
Locale: en_GB
Format for Date & Time: Results in:
IntlDateFormatter::FULL Friday, 5 August 2022 at 15:26:37 Central European Summer Time
IntlDateFormatter::LONG 5 August 2022 at 15:26:37 CEST
IntlDateFormatter::MEDIUM 5 Aug 2022, 15:26:37
IntlDateFormatter::SHORT 05/08/2022, 15:26
Locale: de_DE
Format for Date & Time: Results in:
IntlDateFormatter::FULL Freitag, 5. August 2022 um 15:26:37 Mitteleuropäische Sommerzeit
IntlDateFormatter::LONG 5. August 2022 um 15:26:37 MESZ
IntlDateFormatter::MEDIUM 05.08.2022, 15:26:37
IntlDateFormatter::SHORT 05.08.22, 15:26
Locale: fr_FR
Format for Date & Time: Results in:
IntlDateFormatter::FULL vendredi 5 août 2022 à 15:26:37 heure d’été d’Europe centrale
IntlDateFormatter::LONG 5 août 2022 à 15:26:37 UTC+2
IntlDateFormatter::MEDIUM 5 août 2022 à 15:26:37
IntlDateFormatter::SHORT 05/08/2022 15:26
As salathe has already said, you can also use $formatter->setPattern to further customize the output if needed.
While setlocale() is the correct answer and will still work but is outdated now.
strftime has been DEPRECATED as of PHP 8.1.0 Relying on this function is highly discouraged.
And mentioned Intl extension works perfectly, but not always handy.
One of the simplest ways to work with dates and times is to use Carbon2, CakePHP Chronos or similar library. It provides a single interface to all date's manipulations, formatting, and calculations. If you work with dates a lot I recommend using Carbon and then doing something like this
$date = Carbon::now()->locale('fr_FR');
echo $date->isoFormat('dd DD.MM.YYYY');
Please note that the format differs from the date() one. Full list see in Carbon docs but mentioned D d.m.Y can be something like dd DD.MM.YYYY.
If your project accepts 3rd party libraries, it's really the way to go. Also, if you're using the framework, please check, maybe Carbon (or its wrapper) is already included.
I made something that just does that, because there doesn't seem to exist a simple solution anywhere online, except for with strftime, which is very much deprecated!
My solution extends DateTime::format() with international month and day names and doesn't require installing a bunch of modules, learning new date formatting ways, etc.
After including the classes provided below you can use it as follows. Instead of
$date = new DateTime("2010-01-01 1:23");
echo $date->format("l (D) Y-M-d (F)");
Result: Friday (Fri) 2010-Jan-01 (January)
You can now use
$date = new DateTimeIntl("2010-01-01 1:23");
echo $date->format("l (D) Y-M-d (F)");
Result: vrijdag (vr) 2010-jan.-01 (januari) (Dutch locale).
You can dynamically change the $datetime->locale if wanted.
$date = new DateTimeIntl("2010-01-01 1:23");
$date->locale = "it_IT" ;
echo $date->format("l (D) Y-M-d (F)");
Result: venerdì (ven) 2010-gen-01 (gennaio)
Include this:
class DateTimePatternReplace {
function __construct(public string $DateTimeCode,
public string $IntDateFormatterCode,
public string $tempDateTimePlaceHolder) {}
}
trait addIntlDate {
public string $locale="nl_NL" ; // REPLACE BY YOUR FAVORITE LOCALE
private function getIntResult(string $pattern) {
if ( ! isset($this->formatter) || $this->formatter->getLocale(Locale::VALID_LOCALE) != $this->locale ) {
$this->formatter = new IntlDateFormatter($this->locale);
$this->locale = $this->formatter->getLocale(Locale::VALID_LOCALE); // store the valid version of the locale
}
this->formatter->setPattern($pattern);
return $this->formatter->format($this);
}
function format(string $pattern): string {
// The third parameter can NOT contain normal latin letters, these are random,
// distinctive codes not likely to be in a date format string
$replacePatterns = [/*weekdays*/new DateTimePatternReplace('l', 'EEEE', '[*ł*]'),
new DateTimePatternReplace('D', 'EEE', '[*Đ*]'),
/*month*/ new DateTimePatternReplace('F', 'MMMM', '[*ƒ*]'),
new DateTimePatternReplace('M', 'MMM', '[*μ*]'),
// add new replacements here if needed
] ;
$codesFound=[] ;
foreach($replacePatterns as $p) {
if ( str_contains($pattern, $p->DateTimeCode)) {
// replace codes not prepended by a backslash.
// known bug: codes prepended by double backslashes will not be translated. Whatever.
$pattern = preg_replace('/(?<!\\\)'.preg_quote($p->DateTimeCode)."/", $p->tempDateTimePlaceHolder, $pattern);
$codesFound[] = $p ;
}
}
$result = parent::format($pattern) ;
foreach($codesFound as $p) {
$code = $this->getIntResult($p->IntDateFormatterCode);
$result = str_replace($p->tempDateTimePlaceHolder, $code, $result);
}
return $result ;
}
}
// you can remove this str_contains addition in PHP 8 or higher
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
// end str_contains addition
class DateTimeIntl extends DateTime {
use addIntlDate;
}
class DateTimeImmutableIntl extends DateTimeImmutable {
use addIntlDate;
}
This code extends DateTime and DateTimeImmutable, extends their normal formatting with a locale. So this keeps everything extremely simple
You can add new patterns to be translated if needed by adding codes to the array: a formatting pattern in DateTime::format()-syntax, a corresponding formatting pattern in IntlDateFormatter::format-syntax, plus a placeholder to be used in DateTime::format that does NOT contains letters/codes/patterns that will be used/replaced by the DateTime::format method. See as example the current four codes that use no letters in ASCII lower than 128 letters. (They do use Polish, Greek, Dutch and Slovak letters just for fun.)
Built and tested in PHP 8.1.
For some older version of PHP you will have to change the first class to
class DateTimePatternReplace {
public string $DateTimeCode;
public string $IntDateFormatterCode;
public string $tempDateTimePlaceHolder;
function __construct(string $DateTimeCode, string $IntDateFormatterCode, string $tempDateTimePlaceHolder) {
$this->DateTimeCode = $DateTimeCode;
$this->IntDateFormatterCode = $IntDateFormatterCode;
$this->tempDateTimePlaceHolder = $tempDateTimePlaceHolder;
}
}
This is how I solved combining the features of DateTime and strftime().
The first allows us to manage strings with a weird date format, for example "Ymd" (stored in db from a datepicker).
The second allows us to translate a date string in some language.
For example we start from a value "20201129", and we want end with an italian readable date, with the name of day and month, also the first letter uppercase: "Domenica 29 novembre 2020".
// for example we start from a variable like this
$yyyymmdd = '20201129';
// set the local time to italian
date_default_timezone_set('Europe/Rome');
setlocale(LC_ALL, 'it_IT.utf8');
// convert the variable $yyyymmdd to a real date with DateTime
$truedate = DateTime::createFromFormat('Ymd', $yyyymmdd);
// check if the result is a date (true) else do nothing
if($truedate){
// output the date using strftime
// note the value passed using format->('U'), it is a conversion to timestamp
echo ucfirst(strftime('%A %d %B %Y', $truedate->format('U')));
}
// final result: Domenica 29 novembre 2020
Related
I have a DateTime object which I'm currently formating via
$mytime->format("D d.m.Y")
Which gives me exactly the format I need:
Tue 5.3.2012
The only missing point is the correct language. I need German translation of Tue (Tuesday), which is Die (Dienstag).
This gives me the right locale setting
Locale::getDefault()
But I don't know how to tell DateTime::format to use it.
Isn't there a way to do something like:
$mytime->format("D d.m.Y", \Locale::getDefault());
You can use the Intl extension to format the date. It will format dates/times according to the chosen locale, or you can override that with IntlDateFormatter::setPattern().
A quicky example of using a custom pattern, for your desired output format, might look like.
$dt = new DateTime;
$formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
$formatter->setPattern('E d.M.yyyy');
echo $formatter->format($dt);
Which outputs the following (for today, at least).
Di. 4.6.2013
That's because format does not pay attention to locale. You should use strftime instead.
For example:
setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp())
IntlDateFormatter is the way to go currently (2023).
<?php
$formatter = new IntlDateFormatter(
$locale, // the locale to use, e.g. 'en_GB'
$dateFormat, // how the date should be formatted, e.g. IntlDateFormatter::FULL
$timeFormat, // how the time should be formatted, e.g. IntlDateFormatter::FULL
'Europe/Berlin' // the time should be returned in which timezone?
);
echo $formatter->format(time());
Will give a different output, depending on what you pass as $locale and the date and time format. I wanted to add some samples for future reference. Note that IntlDateFormatter::GREGORIAN and IntlDateFormatter::LONG are interchangable.
Locale: en_US
Format for Date & Time: Results in:
IntlDateFormatter::FULL Friday, August 5, 2022 at 3:26:37 PM Central European Summer Time
IntlDateFormatter::LONG August 5, 2022 at 3:26:37 PM GMT+2
IntlDateFormatter::MEDIUM Aug 5, 2022, 3:26:37 PM
IntlDateFormatter::SHORT 8/5/22, 3:26 PM
Locale: en_GB
Format for Date & Time: Results in:
IntlDateFormatter::FULL Friday, 5 August 2022 at 15:26:37 Central European Summer Time
IntlDateFormatter::LONG 5 August 2022 at 15:26:37 CEST
IntlDateFormatter::MEDIUM 5 Aug 2022, 15:26:37
IntlDateFormatter::SHORT 05/08/2022, 15:26
Locale: de_DE
Format for Date & Time: Results in:
IntlDateFormatter::FULL Freitag, 5. August 2022 um 15:26:37 Mitteleuropäische Sommerzeit
IntlDateFormatter::LONG 5. August 2022 um 15:26:37 MESZ
IntlDateFormatter::MEDIUM 05.08.2022, 15:26:37
IntlDateFormatter::SHORT 05.08.22, 15:26
Locale: fr_FR
Format for Date & Time: Results in:
IntlDateFormatter::FULL vendredi 5 août 2022 à 15:26:37 heure d’été d’Europe centrale
IntlDateFormatter::LONG 5 août 2022 à 15:26:37 UTC+2
IntlDateFormatter::MEDIUM 5 août 2022 à 15:26:37
IntlDateFormatter::SHORT 05/08/2022 15:26
As salathe has already said, you can also use $formatter->setPattern to further customize the output if needed.
While setlocale() is the correct answer and will still work but is outdated now.
strftime has been DEPRECATED as of PHP 8.1.0 Relying on this function is highly discouraged.
And mentioned Intl extension works perfectly, but not always handy.
One of the simplest ways to work with dates and times is to use Carbon2, CakePHP Chronos or similar library. It provides a single interface to all date's manipulations, formatting, and calculations. If you work with dates a lot I recommend using Carbon and then doing something like this
$date = Carbon::now()->locale('fr_FR');
echo $date->isoFormat('dd DD.MM.YYYY');
Please note that the format differs from the date() one. Full list see in Carbon docs but mentioned D d.m.Y can be something like dd DD.MM.YYYY.
If your project accepts 3rd party libraries, it's really the way to go. Also, if you're using the framework, please check, maybe Carbon (or its wrapper) is already included.
I made something that just does that, because there doesn't seem to exist a simple solution anywhere online, except for with strftime, which is very much deprecated!
My solution extends DateTime::format() with international month and day names and doesn't require installing a bunch of modules, learning new date formatting ways, etc.
After including the classes provided below you can use it as follows. Instead of
$date = new DateTime("2010-01-01 1:23");
echo $date->format("l (D) Y-M-d (F)");
Result: Friday (Fri) 2010-Jan-01 (January)
You can now use
$date = new DateTimeIntl("2010-01-01 1:23");
echo $date->format("l (D) Y-M-d (F)");
Result: vrijdag (vr) 2010-jan.-01 (januari) (Dutch locale).
You can dynamically change the $datetime->locale if wanted.
$date = new DateTimeIntl("2010-01-01 1:23");
$date->locale = "it_IT" ;
echo $date->format("l (D) Y-M-d (F)");
Result: venerdì (ven) 2010-gen-01 (gennaio)
Include this:
class DateTimePatternReplace {
function __construct(public string $DateTimeCode,
public string $IntDateFormatterCode,
public string $tempDateTimePlaceHolder) {}
}
trait addIntlDate {
public string $locale="nl_NL" ; // REPLACE BY YOUR FAVORITE LOCALE
private function getIntResult(string $pattern) {
if ( ! isset($this->formatter) || $this->formatter->getLocale(Locale::VALID_LOCALE) != $this->locale ) {
$this->formatter = new IntlDateFormatter($this->locale);
$this->locale = $this->formatter->getLocale(Locale::VALID_LOCALE); // store the valid version of the locale
}
this->formatter->setPattern($pattern);
return $this->formatter->format($this);
}
function format(string $pattern): string {
// The third parameter can NOT contain normal latin letters, these are random,
// distinctive codes not likely to be in a date format string
$replacePatterns = [/*weekdays*/new DateTimePatternReplace('l', 'EEEE', '[*ł*]'),
new DateTimePatternReplace('D', 'EEE', '[*Đ*]'),
/*month*/ new DateTimePatternReplace('F', 'MMMM', '[*ƒ*]'),
new DateTimePatternReplace('M', 'MMM', '[*μ*]'),
// add new replacements here if needed
] ;
$codesFound=[] ;
foreach($replacePatterns as $p) {
if ( str_contains($pattern, $p->DateTimeCode)) {
// replace codes not prepended by a backslash.
// known bug: codes prepended by double backslashes will not be translated. Whatever.
$pattern = preg_replace('/(?<!\\\)'.preg_quote($p->DateTimeCode)."/", $p->tempDateTimePlaceHolder, $pattern);
$codesFound[] = $p ;
}
}
$result = parent::format($pattern) ;
foreach($codesFound as $p) {
$code = $this->getIntResult($p->IntDateFormatterCode);
$result = str_replace($p->tempDateTimePlaceHolder, $code, $result);
}
return $result ;
}
}
// you can remove this str_contains addition in PHP 8 or higher
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
// end str_contains addition
class DateTimeIntl extends DateTime {
use addIntlDate;
}
class DateTimeImmutableIntl extends DateTimeImmutable {
use addIntlDate;
}
This code extends DateTime and DateTimeImmutable, extends their normal formatting with a locale. So this keeps everything extremely simple
You can add new patterns to be translated if needed by adding codes to the array: a formatting pattern in DateTime::format()-syntax, a corresponding formatting pattern in IntlDateFormatter::format-syntax, plus a placeholder to be used in DateTime::format that does NOT contains letters/codes/patterns that will be used/replaced by the DateTime::format method. See as example the current four codes that use no letters in ASCII lower than 128 letters. (They do use Polish, Greek, Dutch and Slovak letters just for fun.)
Built and tested in PHP 8.1.
For some older version of PHP you will have to change the first class to
class DateTimePatternReplace {
public string $DateTimeCode;
public string $IntDateFormatterCode;
public string $tempDateTimePlaceHolder;
function __construct(string $DateTimeCode, string $IntDateFormatterCode, string $tempDateTimePlaceHolder) {
$this->DateTimeCode = $DateTimeCode;
$this->IntDateFormatterCode = $IntDateFormatterCode;
$this->tempDateTimePlaceHolder = $tempDateTimePlaceHolder;
}
}
This is how I solved combining the features of DateTime and strftime().
The first allows us to manage strings with a weird date format, for example "Ymd" (stored in db from a datepicker).
The second allows us to translate a date string in some language.
For example we start from a value "20201129", and we want end with an italian readable date, with the name of day and month, also the first letter uppercase: "Domenica 29 novembre 2020".
// for example we start from a variable like this
$yyyymmdd = '20201129';
// set the local time to italian
date_default_timezone_set('Europe/Rome');
setlocale(LC_ALL, 'it_IT.utf8');
// convert the variable $yyyymmdd to a real date with DateTime
$truedate = DateTime::createFromFormat('Ymd', $yyyymmdd);
// check if the result is a date (true) else do nothing
if($truedate){
// output the date using strftime
// note the value passed using format->('U'), it is a conversion to timestamp
echo ucfirst(strftime('%A %d %B %Y', $truedate->format('U')));
}
// final result: Domenica 29 novembre 2020
Locale specific dates present an interesting problem, not only do we need to specify the appropriate translation (Ex: September=>septembre) but we also need to ensure the format is in the right order (Ex: September 15, 2018=>15 septembre 2018)
PHP's date function date() is English only, so that can be immediately disregared.
Instead, my "research" has pointed me to strftime(). I prepared the following code: echo strftime('%b %e, %Y',strtotime($date));
This outputs: May 1, 2018 and mai 1, 2018. This seemed to solve the issue, that is until we had our bilingual testers identify that the days should be formatted 1 mai 2018.
What is the best way of dealing with this, is it a static class? I want to prepare our solution following best practices, but I have not found the answer.
class LocaleDate
{
public static function date($date)
{
$locale = setlocale(LC_ALL, 0);
if ($locale == "fr_CA" || $locale == "fr_CA.UTF-8") {
return strftime('%e %b %Y',strtotime($date));
} else {
return strftime('%b %e, %Y',strtotime($date));
}
}
}
echo LocaleDate::date($date)
Would that be the best way to go about this issue? Needing to identifty the locale and outputting from there? Seems tedious if you have multiple languages to deal with.
Use the DateTime class alongside the IntlDateFormatter.
Example:
<?php
$dt = new DateTime;
$formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
$formatter->setPattern('E d.M.yyyy');
echo $formatter->format($dt);
Output:
jeu. 17.5.2018
Have a play with it here: https://3v4l.org/tuEM0
Check the documentation here: http://php.net/manual/en/class.intldateformatter.php
I have a function that reads out the date in a file on the first line. This date is formatted in dutch like this 2 mei 2013 or 28 jun. 2013
It needs to convert the date string into a timestamp, but whatever i try it won't work for the mei moths or any other dutch named month. Here is the code I currently have (the original function is a bit more code, but this is where it goes wrong)
function getTimestamp($date){
date_default_timezone_set('Europe/Amsterdam');
setlocale(LC_ALL, 'nl_NL');
$timestamp = strtotime($date);
return $timestamp;
}
Now, here are some results when using this function:
$timestamp = getTimestamp('28 jun. 2013') //1372370400
$timestamp2 = getTimestamp('2 mei 2013') // false
but, when i put this code in the function
echo strftime('%e %b %Y', 1367445600)."\n";
it prints '2 mei 2013'
How can I tell php not only format the date-time string in Dutch, but also read it in Dutch?
=======================
Thanks to some explanation below I now have the code working (this is the full function)
public function getReportDate(){
$mothsTranslated = array('mrt'=> 'mar','mei'=>'may', 'okt'=>'oct');
$content = file($this->file);
$line = $content[0];
$header = str_getcsv($line, $this->delimiter);
$date = str_replace('.', '', $header[1]);
foreach ($mothsTranslated as $dutch => $eng) {
if(strpos($date, $dutch) !== false){
$date = str_replace($dutch, $eng, $date);
}
}
$timestamp = strtotime($date);
return $timestamp;
}
Without creating your own date parser, the native PHP functions only use English dates.
However, there is an international dateformatter extension available for PHP. You can install this plugin and then would be able to parse non-english dates.
http://www.php.net/manual/en/intldateformatter.parse.php
As others found out, strtotime does not respect the set locale.
Indeed, it's description in the manual states: "Parse about any English textual datetime description into a Unix timestamp"
Solutions
You can use strptime() since PHP5 that does respect the locale (like strftime), but there are some warnings about using it on the php website.
You could write a function that replaces the Dutch month names to English month names and then calls strtotime.
Do you know how I can convert this to a strtotime, or a similar type of value to pass into the DateTime object?
The date I have:
Mon, 12 Dec 2011 21:17:52 +0000
What I've tried:
$time = substr($item->pubDate, -14);
$date = substr($item->pubDate, 0, strlen($time));
$dtm = new DateTime(strtotime($time));
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');
The above is not correct. If I loop through a lot of different dates its all the same date.
You don't need to turn the string into a timestamp in order to create the DateTime object (in fact, its constructor doesn't even allow you to do this, as you can tell). You can simply feed your date string into the DateTime constructor as-is:
// Assuming $item->pubDate is "Mon, 12 Dec 2011 21:17:52 +0000"
$dt = new DateTime($item->pubDate);
That being said, if you do have a timestamp that you wish to use instead of a string, you can do so using DateTime::setTimestamp():
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime();
$dt->setTimestamp($timestamp);
Edit (2014-05-07):
I actually wasn't aware of this at the time, but the DateTime constructor does support creating instances directly from timestamps. According to this documentation, all you need to do is prepend the timestamp with an # character:
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime('#' . $timestamp);
While #drrcknlsn is correct to assert there are multiple ways to convert a time string to a datatime, it's important to realize that these different ways don't deal with timezones in the same way.
Option 1 : DateTime('#' . $timestamp)
Consider the following code :
date_format(date_create('#'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
The strtotime bit eliminates the time zone information, and the date_create function assumes GMT.
As such, the output will be the following, no matter which server I run it on :
2011-12-12T13:17:52+00:00
Option 2 : date_create()->setTimestamp($timestamp)
Consider the following code :
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
You might expect this to produce the same output. However, if I execute this code from a Belgian server, I get the following output :
2011-12-12T14:17:52+01:00
Unlike the date_create function, the setTimestamp method assumes the time zone of the server (CET in my case) rather than GMT.
Explicitly setting your time zone
If you want to make sure your output matches the time zone of your input, it's best to set it explicitly.
Consider the following code :
date_format(date_create('#'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Now, also consider the following code :
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Because we explicitly set the time zone of the output to match that of the input, both will create the same (correct) output :
2011-12-12T21:17:52+08:00
Probably the simplest solution is just:
DateTime::createFromFormat('U', $timeStamp);
Where 'U' means Unix epoch. See docs: http://php.net/manual/en/datetime.createfromformat.php
This is my solution:
function changeDateTimezone($date, $from='UTC', $to='Asia/Tehran', $targetFormat="Y-m-d H:i:s") {
$date = new DateTime($date, new DateTimeZone($from));
$date->setTimeZone(new DateTimeZone($to));
return $date->format($targetFormat);
}
I try to calculate the easter date in php.
echo(date("2012: t.n.Y", easter_date(2012)).'<br>'); // 2012: 30.4.2012
This date is correct for the eastern orthodox churches. But I want the normal one!
My next try with the easter_days function:
function easter($year) {
$date = new DateTime($year.'-03-21');
$date->add(new DateInterval('P'.easter_days($year).'D'));
echo $year.": ".$date->format('t.m.Y') . "<br>\n";
}
easter(2012); // 2012: 30.4.2012
Tested oh PHP 5.2.6 and 5.3.6. I also tried to change the timezone with no success.
Your date format is wrong. t is the number of days in the given month (april = 30). Use d for day of the month:
echo(date("d.m.Y", easter_date(2012)).'<br>');
// will output: 08.04.2012
btw: orthodox easter date is April 15th this year.
If you want to use the DateTime class, the following will give you a DateTime object set to Easter. Use easter_date() instead of fiddling around with easter_days():
function easter($year, $format = 'd.m.Y') {
$easter = new DateTime('#' . easter_date($year));
// if your timezone is already correct, the following line can be removed
$easter->setTimezone(new DateTimeZone('Europe/Berlin'));
return $easter->format($format);
}
echo easter(2012); // 08.04.2012
echo easter(2012, 'd.m.Y H:i'); // 08.04.2012 00:00
Timezone
Setting the timezone is only necessary when the default timezone is wrong. Must be set afterwards as it is ignored in the constructor when a unix timestamp is provided.
If left out, the DateTime constructor may produce a wrong date (e.g. 07.04.2012 22:00 for 2012 instead of 08.04.2012 00:00)