在php中使用日期格式时出现意外答案

问题描述 投票:0回答:1

我想从约会开始月份,这些日期是从数据库中获取的,所以有些日期是'0000-00-00'格式。

//例如我在下面给出了这个日期

$arr='0000-00-00';
$thismonth = date('m', strtotime($arr));
echo $thismonth;

//这里我期待00或null,但答案是11。

php
1个回答
1
投票

date永远不是'0000-00-00'或0或null,在日期函数中你永远不会输入错误值,所以检查

$arr='0000-00-00';
if($arr != '0000-00-00'){
$thismonth = date('m', strtotime($arr));
echo $thismonth;
}
else{
$thismonth = '00';
}
© www.soinside.com 2019 - 2024. All rights reserved.