I read the
Patch topic
to the Drupal-module Date - we show the months in the genitive case , and decided to make a decision, too, aimed at eradicating the tongue-tiedness in the derivation of dates. Drupal is not used by anyone using PHP, but any PHP 5.2+ user can use my solution.
I wrote a class that uses the capabilities of the built-in PHP class DateTime, which allows you to add your own functions for formatting, while own formatters take precedence over the built-in ones. For example, the task with months can be solved by setting your own formatter for the key “F”, like this:
')
$ idt = new IDateTime ();
$ months = array (1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August',
9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');
// override "F"
$ idt-> addFormatter ('F', function ($ d) use ($ months) {
return $ months [$ d-> format ('n')];
});
echo $ idt-> format ('j F Y'); // March 1, 2010
The formatter is the name of the function, an array (class, method), an array (object, method) or Closure (if you have php-5.3 +). 1 argument is passed to the formatter - this is a DateTime object.
In the same way, you can set the output of the days of the week, and you can, let's say, make your own key, which is not in date () formats. For example, instead of "{season}" substitute "spring", "summer", "autumn", "winter".
Implementation: I made a wrapper around the built-in datetime. At first, I just inherited my class from DateTime and the implementation was even easier than now. Unfortunately, with some fright began to emerge segfolt and the concept had to be changed.
UPD: with the
inkover feed,
I slightly optimized the formatting process.
Download the source code of the class here:
http://vbolshov.org.ru/samples/idatetime/IDateTime.phps