错误如果条件不适用于PHP 2月29日,则当我将日期设置为2月29日时,它将显示第一个条件true1

时间:2018-08-12 06:18:28

标签: php

2月28日至29日之间的问题,请更改日期并查看结果

$cmncdate = '2019-02-29';

$time=strtotime($cmncdate);

$day=date("d",$time);

$mnth=date("m",$time);

if ($day>=1 AND $day<11)

    { $nday = '10'; $newDate = date("Y-m-$nday", strtotime($cmncdate)); 

    echo $newDate; }

elseif ($day>10 AND $day<21)

    { $nday = '20'; $newDate = date("Y-m-$nday", strtotime($cmncdate)); 

    echo $newDate; }

elseif ($day>20 AND $day<31 AND $mnth!=2)

    { $nday = '30';  $newDate = date("Y-m-$nday", strtotime($cmncdate)); 

    echo $newDate; }

elseif ($day>20 AND $day<29 AND $mnth=2)

    { $nday = '28'; $newDate = date("Y-m-$nday", strtotime($cmncdate)); 

    echo $newDate; }

    elseif ($day=29 AND $mnth=2)

    { $nday = '29';  $newDate = date("Y-m-$nday", strtotime($cmncdate)); 

    echo $newDate; }

1 个答案:

答案 0 :(得分:0)

问题在于2019-02-29是无效日期。第29个仅在leap年(2012、2016、2020等)上有效。因此日期会自动移至1/3/2019 ...

$cmncdate = '2019-02-29';
$time=strtotime($cmncdate);
$day=date("d",$time);
$mnth=date("m",$time);
echo $mnth.".".$day;

给予...

03.01

因此,尝试使用2020年...

$cmncdate = '2020-02-29';
$time=strtotime($cmncdate);
$day=date("d",$time);
$mnth=date("m",$time);
echo $mnth.".".$day;