我有一个简单的问题,因为我只想将时间(以数组格式)转换为24小时的时间字符串。我从用户输入获得时间的问题,它将它放在格式化不工作的数组对象中。 我无法从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);
答案 0 :(得分:1)
您应该创建时间实例,因为$this->request->data['start_time']
未格式化:
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');
*经过测试和工作。