我输入的日期没有正确显示

时间:2015-05-22 02:27:22

标签: php date strtotime

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

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

1 个答案:

答案 0 :(得分:1)

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 ,因为 false 返回值来自 strtotime()

如果您一直想使用 d / m / Y 格式。您可以使用 str_replace()轻松地将/替换为-

$date = str_replace('/', '-', $date);

因此,它将被正确评估为 d-m-Y