mercredi 20 novembre 2019

How to check if a (made-up) store is open or closed between specific times just with an if/elseif statement?

For a small PHP assignment for school (Using MAMP to display our page on localhost:8888) we had to create a page that displayed the following:

  1. The date and time of today.
  2. Create a If-else-elseif statement that checks if our fictive store was open between Monday - Friday (Closed on saturday and sunday), and open between 08:30 and 17:00, while displaying as closed outside of those times

Approach number 1, that worked but I wasn't allowed to combine 'H' and 'i' together in date("Hi").

<?php

 $datum = date('D');
 $uren = date('H');
 $minuten = date('i');

 if($datum == 'Sat' || $datum == 'Sun') { 
          echo "The store isn't open in the weekends";
       } 
        elseif(date('Hi') < 830 || date("Hi") > 1700) {
            echo "the store is closed";
       } 
       else {
        echo "The store is open";
        }
    ?>

I had to do the hours and minutes seperately, so: first iterate through the hours, then through the minutes. Approach 2:

 <?php 
     $datum = date('N');
     $uren = date('H');
     $minuten = date('i');

          if($datum > 5) {
             echo "The store isn't open in the weekend";
            } elseif ($uren < 8 || $uren > 17) {
              echo "The store is closed";
            } elseif ($uren == 8 && $minuten < 30) {
              echo "The store is closed";
            } else {
                echo "The store is open";
                }

    ?>

According to my teacher, the overal setup seems to be somewhat alright, but I can't seem to figure out how I can iterate between the 2 hours (8 and 17) and ALSO the minutes (08:30 and 17:00). Not sure if the above example is already going in the proper direction? but it is not working for me at least.

My last attempt

   <?php 
     $datum = date('N');
     $uren = date('H');
     $minuten = date('i');

     if($datum > 5) {
             echo "The store is not open in the weekends";
       } elseif($uren > 17 && ($uren < 8 || $uren === 8 && $minuten < 30)){
              echo "The store is closed";
            }
           else {
                   echo "The store is open";
                }
}

When I add <?php echo "The time is " . date("H:i:s"); ?> and change the "H:i:s" values, the "It's open/closed" doesn't seem to respond to it (unless it's not the way to hardcode time).

What do i need to fix in my if-elseif-statement to make the very simple "Is it open"-checker work?

Aucun commentaire:

Enregistrer un commentaire