cakephp 2.3格式时间输入小时分钟秒

时间:2013-08-09 09:41:35

标签: cakephp

我正在尝试格式化输入而没有任何成功。我需要用每个音频文件的持续时间将mp3保存到我的表中。为此,我希望有一个输入,允许我选择小时:分钟:秒。我表中持续时间的字段是TIME,我猜不需要dateTime。

所以这就是我所做的不起作用的事情:

<?php echo $this->Form->input('duration', array('type' => 'time', 'label' => 'Durée piste', 'dateFormat' => 'H:i:s')); ?>

有谁知道怎么做? 提前谢谢!

2 个答案:

答案 0 :(得分:3)

如果您不是PHP专家,那么您可以使用下面给出的简单代码。

注意:发布是我的模特名称。所以将其替换为你的。

在控制器中添加功能

public function add() {
        if ($this->request->is('post')) {
            $this->Post->create();

            $hours   = $this->data['Post']['hours'];
            $minutes = $this->data['Post']['minutes'];
            $seconds = $this->data['Post']['seconds'];

        $this->request->data['Post']['duration']=$hours.':'.$minutes.':'.$seconds;


            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash('Your post has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your post.');
            }
        }
    }

add.ctp文件中的代码

$hours=array();
for($i=0;$i<13;$i++){
  $hours[$i]=$i;
}

$ms=array();
for($i=0;$i<60;$i++){
  $ms[$i]=$i;
}

echo $this->Form->input('hours',array('type' => 'select', 'options' => $hours));
echo $this->Form->input('minutes',array('type' => 'select', 'options' => $ms));
echo $this->Form->input('seconds',array('type' => 'select', 'options' => $ms));

答案 1 :(得分:0)

只需用这些新阵列替换旧阵列。

$hours=array();

for($a=0; $a < 2; $a++){
    for($b=0; $b < 10; $b++){
        if ($a.$b == 13) {
            break;    
        }
        $hours[$a.$b]=$a.$b;

    }
}

$ms=array();

for($a=0; $a < 6; $a++){
    for($b=0; $b < 10; $b++){
        $ms[$a.$b]=$a.$b;
    }
}