[php]判断日期格式是否合法

/*判断函数*/
function isDate( $dateString ) {
  return strtotime( date('Y-m-d', strtotime($dateString)) ) === strtotime( $dateString );
/*date函数会给月和日补零,所以最终用unix时间戳来校验*/
} 
/*测试数据*/
echo $this->isDate('2014-11-19') ? 'true' : 'false';   /*true*/
echo $this->isDate('2014-11-32')? 'true' : 'false';    /*false*/
echo $this->isDate('2014-a-b')? 'true' : 'false';      /*false*/
echo $this->isDate('2014-1-1')? 'true' : 'false';      /*true*/
echo $this->isDate('2014-01-01')? 'true' : 'false';    /*true*/

你可能感兴趣的:([php]判断日期格式是否合法)