jagguy
jagguy

Reputation: 183

time array converson cakephp3

I have a simple question as I just want to convert the time (in array format) to a 24hr time string. The issue I get the time from user input and it places it in an array object which the formatting isnt working . I couldnt find the answer from https://book.cakephp.org/3.0/en/core-libraries/time.html#conversion

time format inputted from a form
'start_time' => [
        'hour' => '02',
        'minute' => '00',
        'meridian' => 'pm'
    ],

view//
   echo $this->Form->input('start_time', ['label' => 'Class Start Time','type' => 'time',
                  'interval' => 5,'timeFormat'=>12,'value'=>$startTime,]); 



 //controller   
      if ($this->request->is('post')) {

        debug($this->request->data['start_time']->i18nFormat('HH:mm:ss'));//cant use on an array



  //this works but is there   a better way
 $startTime = $this->request->data['start_time']['hour'].":".$this->request->data['start_time']['minute']." ". 
                      $this->request->data['start_time']['meridian'];
              $this->request->data['start_time'] = date("H:i:s", strtotime( $startTime)); 
                debug($this->request->data);

Upvotes: 0

Views: 69

Answers (1)

Alan Delval
Alan Delval

Reputation: 479

You should create a Time instance, because $this->request->data['start_time'] is not formated:

use Cake\I18n\Time;
...
$StartTimeHour = $this->request->data['start_time']['hour'];
$StartTimeMinute = $this->request->data['start_time']['minute'];
$StartTimeMeridian = $this->request->data['start_time']['meridian'];

$time = new Time("${StartTimeHour}:${StartTimeMinute} ${StartTimeMeridian}");

echo $time->i18nFormat('HH:mm:ss');

*Tested and working.

Upvotes: 1

Related Questions