Skip to content
Advertisement

How can getdate[“weekday”] start from Monday instead of sunday?

I need to start the “weekday” of getdate() to Monday instead of Sunday, is there a way to do this?

Here is my code, don’t worry about the parameters in validatePlanningVendeur the problem is when i try to get the current date arret, so that it can validate from Monday till arret.

You might notice i wrote $arret = $today["wday"]-1;,this is my attempt to bypass the wkday Sunday = 0 so that Monday is 0, but now it does not work for Sunday because it is equal to -1.

To get it short, right now $arret work from Monday to Saturday only.

function validatePlanningVendeur($week, $vendeur, $boutique, $type_connecte, $user_connecte){
        $connexion = connexion();
        // Getting the current date in order to get the end
        $today = getdate();
        $arret = $today["wday"]-1; // attempt to bypass sunday = 0
        echo($arret);
        // echo("Aujourdhui : " . $today["wday"]);
        // echo("arret: " . $arret);

        // We get every day of the week until today ($arret)
        for($i = 0; $i <= $arret ; $i++){
            // On crée le year-month-day pour chaque jour de la semaine
            // echo($week[$i]);
            $dateCurrent = getdate($week[$i]);
            $day = $dateCurrent['mday'];
            $month = $dateCurrent['mon'];
            $year = $dateCurrent['year'];

            // Used to fetch from DB
            $jour = $year. '-' . $month . '-' . $day;
            // echo("Jour : " . $jour . " ");

            if($type_connecte == 1){ //Si c'est un RPV qui est connecté
                // On assigne id_validation = 1
                $request_validate = $connexion->prepare("UPDATE plannings SET id_validation = ? WHERE date_planning = ? AND id_employe = ? AND id_validation = ? AND id_boutique = ?");
                $request_validate->execute(array(1, $jour, $vendeur, 0, $boutique));
            }else{// On ne prend pas en compte la valeur boutique pour le RC
                $request_validate = $connexion->prepare("UPDATE plannings SET id_validation = ? WHERE date_planning = ? AND id_employe = ? AND id_validation = ?");
                $request_validate->execute(array(1, $jour, $vendeur, 0,));
            }
        }
}

Advertisement

Answer

Try to use this:

$today = new DateTime();
// Get ordinal number of the day of the week by ISO 8601.
// It starts from 1 (Monday) to 7 (Sunday).
$arret = $today->format('N'); // Today is 7th day
for ($i = 0; $i <= $arret - 1 ; $i++) {
 // do something
}

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement