简体   繁体   中英

How to put multiple interval “new DateTime” (php) inside a condition

I would like to display the opening hours of a sport center (association). This code must concern the school year 2019-2020. By default (else) it display the opening yours, in first condition(if) it display the school vacation and in second condition (else if) it display holidays.

For one interval of "new Datetime" it works fine. As soon as I put multiple interval it doesn't work and I think its normal . But i can't find the good way to put multiple interval of datetime.

Could I use dateInterval ?

$TodayDate->getTimestamp() > $automne1->getTimestamp() && $TodayDate->getTimestamp() < $automne2->getTimestamp()

function whatday() {

     //vac automne
     $automne1 = new DateTime('2019-10-19');
     $automne2  = new DateTime('2019-11-03');

     //vac noel - voir horaires exceptionnels plus bas
     $noel1 = new DateTime('2019-12-21');
     $noel2 = new DateTime('2020-01-05');

     // vac hiver
     $hiver1 = new DateTime('2020-02-08');
     $hiver2 = new DateTime('2020-02-23');

     //vac printemps
     $printemps1 = new DateTime('2020-04-04');
     $printemps2 = new DateTime('2020-04-19');

     // vac été
     $ete1 = new DateTime('2020-07-04');
     $ete2 = new DateTime('2020-07-13');
     $ete3 = new DateTime('2020-07-15');
     $ete4 = new DateTime('2020-08-14');
     $ete5 = new DateTime('2020-08-16');

     //rentrée sco 2020
     $ete6 = new DateTime('2020-09-01');

     //jours fériés
     $armistice18 = new DateTime('2019-11-11');
     $epiphanie = new DateTime('2020-01-06');
     $fetetravail = new DateTime('2020-05-01');
     $victoire45 = new DateTime('2020-05-08');
     $ascension = new DateTime('2020-05-21');
     $lunpentecote = new DateTime('2020-06-01');
     $14juillet = new DateTime('2020-07-14');
     $asomption = new DateTime('2020-08-15');

     // Horaires execptionnels noel du 24 dec au 5 janvier

     // conditions vacances scolaires
     if (
     $TodayDate->getTimestamp() > $automne1->getTimestamp() && $TodayDate->getTimestamp() < $automne2->getTimestamp() &&
     $TodayDate->getTimestamp() > $noel1->getTimestamp() && $TodayDate->getTimestamp() < $noel2->getTimestamp() &&
     $TodayDate->getTimestamp() > $hiver1->getTimestamp() && $TodayDate->getTimestamp() < $hiver2->getTimestamp() &&
     $TodayDate->getTimestamp() > $printemps1->getTimestamp() && $TodayDate->getTimestamp() < $printemps2->getTimestamp() &&
     $TodayDate->getTimestamp() > $automne1->getTimestamp() && $TodayDate->getTimestamp() < $automne2->getTimestamp() &&
     $TodayDate->getTimestamp() > $ete1->getTimestamp() && $TodayDate->getTimestamp() < $ete2->getTimestamp() &&
     $TodayDate->getTimestamp() > $ete3->getTimestamp() && $TodayDate->getTimestamp() < $ete4->getTimestamp() &&
     $TodayDate->getTimestamp() > $ete5->getTimestamp() && $TodayDate->getTimestamp() < $ete6->getTimestamp()
     )
     {
     if($NameDay === 'Mon')
     return "8h00 à 21h30";
     if($NameDay === 'Tue')
     return "12h00 à 21h30";
     if($NameDay === 'Wed')
     return "8h00 à 21h30";
     if($NameDay === 'Thu')
     return "12h00 à 21h30";
     if($NameDay === 'Fri')
     return "8h00 à 21h30";
     if($NameDay === 'Sat')
     return "8h00 à 19h00";
     if($NameDay === 'Sun')
     return "8h00 à 12h00";
     }
     // conditions jours fériés et vac noel
     else if(
     $TodayDate->getTimestamp() === $armistice18->getTimestamp() &&
     $TodayDate->getTimestamp() === $epiphanie->getTimestamp() &&
     $TodayDate->getTimestamp() === $fetetravail->getTimestamp() &&
     $TodayDate->getTimestamp() === $victoire45->getTimestamp() &&
     $TodayDate->getTimestamp() === $ascension->getTimestamp() &&
     $TodayDate->getTimestamp() === $lunpentecote->getTimestamp() &&
     $TodayDate->getTimestamp() === $14juillet->getTimestamp() &&
     $TodayDate->getTimestamp() === $asomption->getTimestamp() &&
     )
     {
     return "FERMÉEE";
     }
     // conditions horaires normaux
     else{

     if($NameDay === 'Mon')
     return "8h00 à 21h30";
     if($NameDay === 'Tue')
     return "8h00 à 21h30";
     if($NameDay === 'Wed')
     return "8h00 à 21h30";
     if($NameDay === 'Thu')
     return "8h00 à 21h30";
     if($NameDay === 'Fri')
     return "8h00 à 21h30";
     if($NameDay === 'Sat')
     return "08h00 à 19h00";
     if($NameDay === 'Sun')
     return "08h00 à 12h00";
     }
     */
};
add_shortcode('opendays', 'whatday');

In header.php

I put :

I try tu use "&&" between others interval of days but I think it's not the good solution.

I have resolved my problem/ I replaced Logical Operators && by || in multiple comparison of two dates.

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