Skip to content

Instantly share code, notes, and snippets.

@xavier83ar
Last active December 6, 2018 03:29
Show Gist options
  • Select an option

  • Save xavier83ar/bcb7ef815cae573f918730d00e0613e0 to your computer and use it in GitHub Desktop.

Select an option

Save xavier83ar/bcb7ef815cae573f918730d00e0613e0 to your computer and use it in GitHub Desktop.
Cakephp 3 translated month names in Form helper
<?php
/**
* File: src/View/Widget/DateTimeWidget.php
* Overwriting this file should be enough, as cakephp class look up system would find this class before the framework's one.
*/
namespace App\View\Widget;
use Cake\I18n\FrozenDate;
use Cake\View\Widget\DateTimeWidget as CakePhpDateTimeWidget;
class DateTimeWidget extends CakePhpDateTimeWidget
{
/**
* Override this method to get month name translated by system according to the lang config value.
*
* {{@inheritdoc}}
*/
protected function _getMonthNames($leadingZero = false)
{
$_time = new FrozenDate();
$_format = 'MMMM';
$_monthNumbers = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
$months = [];
foreach ($_monthNumbers as $key => $number) {
$months[$leadingZero ? $number : ($key + 1)] = ucfirst($_time->month($key + 1)->i18nFormat($_format));
}
return $months;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment