0

I did run it in w3school.com, it return a string "06/12/2021,13/12/2021,20/12/2021,27/12/2021". It is what i want. I couldn't find any error in my code but it return only "12“ when i run in my website.That means the line "foreach (getWeekdays2m(date("Y"),date("m")) as $weekdayArray) “ has problem. Please kindly help.

   <?php
$stringweekday = "";
function getWeekdays2m($y, $m) {
    echo "1";
    //$b = (int)$_POST[b];
    $b = 1;
    $m = $m+$b;
    //$Day_of_class = $_POST[Day_of_class];
    $Day_of_class = "monday";
    $firstDay = "first $Day_of_class of $y-$m";
    
    
    if ($b==1) {
        
        echo "2";
        return new DatePeriod(new DateTime($firstDay),DateInterval::createFromDateString('+1 week'),new DateTime("last day of $y-$m"));
    }
    else {
        echo "3";
        return new DatePeriod(new DateTime($firstDay),DateInterval::createFromDateString('+1 week'),new DateTime("last day of next month $y-$m"));
        
    }
}

foreach (getWeekdays2m(date("Y"),date("m")) as $weekdayArray) {
    echo "4";
    if ($stringweekday == "") {
        echo "5";
        $stringweekday = $weekdayArray->format("d/m/Y");
    }
    else {
        echo "6";
        $stringweekday = $stringweekday.",".$weekdayArray->format("d/m/Y");
    }
}
echo "7";
echo $stringweekday;
?>

enter image description here

Eric Chong
  • 495
  • 1
  • 7
  • 21
  • Set error reporting on and/or check error log. – pavel Nov 07 '21 at 14:08
  • 1
    Please format your code to make it readable here on SO – M. Eriksson Nov 07 '21 at 14:12
  • 1
    In the top of the script, `$stringweekday == "";` should be `$stringweekday = "";`. If you use two equal signs, you're comparing the undefined variable `$stringweekday` with an empty string, not setting it. That should throw some undefined variable warnings. While developing, make sure you [display all errors and warnings](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Nov 07 '21 at 14:14
  • $stringweekday = ""; amended. it has no error, it just show $stringweekday = "". it suppose to show "06/12/2021,13/12/2021,20/12/2021,27/12/2021" – Eric Chong Nov 07 '21 at 14:47
  • I couldn't find the error log in cpanel. – Eric Chong Nov 07 '21 at 15:03

0 Answers0