日期comparson代码无法找到8月和9月的日期

时间:2017-12-01 14:16:45

标签: php date-comparison

我在这里有一些简单的代码来比较日期和数量产品,除了8月和9月之外,它适用于我所有月份。我已经过了十几次,所有月份的一切看起来都一样,但八月和九月的日期都没有找到,其他日期测试OK。

对不起蹩脚的代码,我还是初学者。

提前致谢

<?php 

//$ship_date = 20170420;
//$ship_date = 20170520;
//$ship_date = 20170620;
//$ship_date = 20170720;
//$ship_date = 20170820;
//$ship_date = 20170920;
//$ship_date = 20171020;
//$ship_date = 20171120;
$ship_date = 20171220;

$apr = '1';
$may = '1';
$june = '1';
$july = '1';
$august = '1';
$september = '1';
$october = '1';
$nov = '1';
$decem = '1';

$valid = "Y";

$case_qty = 1;

$apr_start = 20170401;
$apr_end = 20170430;
$may_start = 20170501;
$may_end = 20170531;
$june_start = 20170601;
$june_end = 20170630;
$july_start = 20170701;
$july_end = 20170731;
$augmonth_start = 20170801;
$augmonth_stop = 20170831;
$september_start = 20170901;
$september_stop = 20170930;
$october_start = 20171001;
$october_end = 20171031;
$nov_start = 20171101;
$nov_end = 20171131;
$decem_start = 20171201;
$decem_end = 20171231;
$jan_start = 20180101;
$jan_end = 20180131;
$feb_start = 20180201;
$feb_end = 20180228;
$mar_start = 20180301;
$mar_end = 20180331;

if ($valid == "Y")
{

    if (($ship_date >= $apr_start) && ($ship_date <= $apr_end))
    {
        echo "Sold apr<br />";

        $apr = $apr + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        //$apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $may_start) && ($ship_date <= $may_end))
    {
        echo "Sold may<br />";

        $may = $may + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        //$may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $june_start) && ($ship_date <= $june_end))
    {
        echo "Sold june<br />";

        $june = $june + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        //$june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $july_start) && ($ship_date <= $july_end))
    {
        echo "Sold july<br />";

        $july = $july + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        //$july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $augmonth_start) && ($ship_date <= $augmonth_end))
    {
        echo "Sold august <br />";

        $august = $august + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;

        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $september_start) && ($ship_date <= $september_end))
    {
        echo "Sold sept <br />";

        $september = $september + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;

        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $october_start) && ($ship_date <= $october_end))
    {
        echo "Sold oct<br />";

        $may = $october + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        //$october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $nov_start) && ($ship_date <= $nov_end))
    {
        echo "Sold nov<br />";

        $nov = $nov + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        //$nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $decem_start) && ($ship_date <= $decem_end))
    {
        echo "Sold decem<br />";

        $decem = $decem + $case_qty;

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        //$decem = 0;
    } elseif (($ship_date >= $jan_start) && ($ship_date <= $jan_end))
    {
        echo "Sold jan<br />";

        $jan = $jan + $case_qty;

        //$jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $feb_start) && ($ship_date <= $feb_end))
    {
        echo "Sold feb<br />";

        $feb = $feb + $case_qty;

        $jan = 0;
        //$feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } elseif (($ship_date >= $mar_start) && ($ship_date <= $mar_end))
    {
        echo "Sold mar<br />";

        $mar = $mar + $case_qty;

        $jan = 0;
        $feb = 0;
        //$mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;
    } else
    {

        echo "No month selected!!!!!!  " . $ship_date . "<br />";

        $jan = 0;
        $feb = 0;
        $mar = 0;
        $apr = 0;
        $may = 0;
        $june = 0;
        $july = 0;
        $august = 0;
        $september = 0;
        $october = 0;
        $nov = 0;
        $decem = 0;


    }
}
echo "Month Totals: " . $jan . " " . $feb . " " . $mar . " " . $apr . " " . $may . " " . $june . " " . $july .
    " " . $august . " " . $september . " " . $october . " " . $nov . " " . $decem . "<br />"; 

?>

1 个答案:

答案 0 :(得分:1)

你有这些变量:

$augmonth_stop = 20170831;
$september_stop = 20170930;

但正在检查不同的东西:

($ship_date <= $augmonth_end)
($ship_date <= $september_end)

修复一个或另一个,你的代码应该有用。

也就是说,考虑使用一些PHP内置日期函数,比如strtotime(),或者更好地为你的应用程序使用DateTime类。