
Beautiful and correct dates in any language
I read the topic Patch for the Date Drupal module - we show the months in the genitive case , and also decided to make a decision aimed at eradicating tongue-tied languages when displaying dates. Drupal is not used by everyone who uses PHP, but any PHP 5.2+ user can use my solution.
I wrote a class that uses the capabilities of the DateTime class built into PHP, which allows you to add your own functions for formatting, while native formatters take precedence over built-in ones. So, for example, the task with months can be solved by setting your formatter for the key "F", like this:
A formatter is a function name, an array (class, method), 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 specify the output of the days of the week, or you can, let's say, make your own key, which is not in the date () formats. For example, instead of "{season}" substitute "spring", "summer", "autumn", "winter".
Implementation: I wrapped around the built-in DateTime. At first, I just inherited my class from DateTime and the implementation was even simpler than it is now. Unfortunately, segolts began to arise from some fright, and the concept had to be changed.
UPD: from the supply of inkover a little optimized formatting process.
You can download the class source here:http://vbolshov.org.ru/samples/idatetime/IDateTime.phps
I wrote a class that uses the capabilities of the DateTime class built into PHP, which allows you to add your own functions for formatting, while native formatters take precedence over built-in ones. So, for example, the task with months can be solved by setting your 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" format key to use russian month names $ idt-> addFormatter ('F', function ($ d) use ($ months) { return $ months [$ d-> format ('n')]; }); echo $ idt-> format ('j F Y'); // March 1, 2010
A formatter is a function name, an array (class, method), 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 specify the output of the days of the week, or you can, let's say, make your own key, which is not in the date () formats. For example, instead of "{season}" substitute "spring", "summer", "autumn", "winter".
Implementation: I wrapped around the built-in DateTime. At first, I just inherited my class from DateTime and the implementation was even simpler than it is now. Unfortunately, segolts began to arise from some fright, and the concept had to be changed.
UPD: from the supply of inkover a little optimized formatting process.
You can download the class source here:http://vbolshov.org.ru/samples/idatetime/IDateTime.phps