简体   繁体   中英

strtotime function not working properly

I'm trying to print only the weekdays with the format of Ymd using this code

$strtdate = "2017-04-10";

$incrmntvar = 0;

while($incrmntvar <= $gettotaldays){
    $checkday = date("D",strtotime($strtdate));

echo "{$checkday} and {$strtdate}". "<br>";
    if($checkday != 'Sat' || $checkday != 'Sun'){

        $incre_date = strtotime("1 day", strtotime($strtdate));
        $strtdate = date("Y-m-d", $incre_date);     
        $incrmntvar ++;


    }

    else{
        $incre_date = strtotime("1 day", strtotime($strtdate));
        $strtdate = date("Y-m-d", $incre_date);
        echo "else " . $strtdate . "<br>";

    }


}

but it prints all the days including the saturday and sunday. What's wrong with my code

In your Sat/Sun day comparison, you're using or ( || ), instead of and ( && ). If $checkday is 'Sat' , then the check of $checkday != 'Sun' will still return true, and thus it will still output the weekend days.

Thus, the following line

if($checkday != 'Sat' || $checkday != 'Sun')

Should be corrected to

if($checkday != 'Sat' && $checkday != 'Sun')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM