1

In cakephp 3.x I have used monthNames attribute for months displayed as numbers by passing false. Like below example.

echo $this->Form->month('mob', ['monthNames' => false]);

In version 4.x , This example code has remove from documentation

https://book.cakephp.org/4/en/views/helpers/form.html#creating-month-controls

How can I displayed monthNames as number. I am able to do like below

<?= $this->Form->select('mob',[1,2,3,4...]) ?>

What is the alternative of monthNames attribute in cakephp 4.x ?

Niloy Rony
  • 602
  • 1
  • 8
  • 23

1 Answers1

1

There is no built-in alternative, the option was just dropped, CakePHP 4 switched date/time related controls to use native input types, given the grown browser adoption.

Thr result was just a select input with values from 1 to 12 anyways, so you can just replicate that. It should be noted though that select options will use array keys as values, and array values as labels, so if you don't want your month value to start with 0, you have to provide keys that start with 1. That is easy enough though:

$options = array_combine(range(1, 12), range(1, 12));

If you wanted to replicate the defaults from CakePHP 3, then you'd need a little more logic, as it used zero padded keys and values - still easy enough:

$options = collection(range(1, 12))
    ->map(function ($val) { return sprintf('%02d', $val); })
    ->indexBy(function ($val) { return $val; })
    ->toArray();
ndm
  • 59,784
  • 9
  • 71
  • 110