日期没有正确显示,因为我输入


if ($this->request->is('post')) {
    $this->Operation->create();
    if(!isset($this->request->data['Operation']['date_alarma'])){
        $this->request->data['Operation']['date_alarma']= date('Y-m-d H:i:s',strtotime($this->request->data['Operation']['date_alarma'].' '.$this->request->data['Operation']['alarmA']['hour'].$this->request->data['Operation']['alarmA']['min']));
    }
    else if(!isset($this->request->data['Operation']['date_alarmb'])){
        $this->request->data['Operation']['date_alarmb']= date('Y-m-d H:i:s',strtotime($this->request->data['Operation']['date_alarmb'].' '.$this->request->data['Operation']['alarmB']['hour'].$this->request->data['Operation']['alarmB']['min']));
    }
    else if(isset($this->request->data['Operation']['date_alarmcc'])){
        pr($this->request->data['Operation']['date_alarmcc']) ;
        $a= date('Y-m-d',strtotime($this->request->data['Operation']['date_alarmcc']));
        pr($a); die;
    }

输出如下所示

30/05/2015
1970-01-01

我可以知道为什么吗?我输入的日期是30/05/2015字段date_alarmcc

strtotime()将把此格式解释为m/d/Y,而不是d/m/Y,因此将返回false,因为没有30个月。

echo (int) strtotime('05/30/2015');
// 1432969200
echo (int) strtotime('30/05/2015');
// 0
然而,

strtotime () 将评估 d-m-Y :

echo (int) strtotime('05-30-2015');
// 0
echo (int) strtotime('30-05-2015');
// 1432969200

Unix时间戳表示从1970-01-01经过的秒数,因此这表示时间戳0,因为strtotime()返回值false

如果您总是想使用d/m/Y格式。您可以使用str_replace():

轻松地将/替换为-
$date = str_replace('/', '-', $date);

因此,它将被正确地计算为d-m-Y

相关内容

  • 没有找到相关文章

最新更新