Question

Print correct dates in PHP

I want to print out different information as shown in my code for each different date(day) of the year, but I seem not to get the answer. Every other day of the week works fine, but the weekends(Sat and Sun) has been a headache. I want to print the correct date on weekends. For example, let’s take this week and next week weekends, I should have the following prints: Happy Weekend, today is Saturday 2019-02-09. And on the next day which is a Sunday, I should print out: Happy Weekend, today is Sunday 2019-02-10. Also next week weekends should give 2019-02-16 and 2019-02-17 respectively on different days of the weekend, but this is not happening. See my code: $date = new DateTime(‘’);
$datei = $date->format(‘Y-m-d’);
$newYear = new DateTime(‘’); $newYear_date = $newYear->format(‘2019-01-01’); $ValentineS = new DateTime(‘’); $valentineS_date = $ValentineS->format(‘2019-02-14’); $WomenDay = new DateTime(‘’); $WomenDay_date = $WomenDay->format(‘2019-03-08’); /* …other days not shown for brevity…/ /…move to the weekends…*/ $sat_begin = new DateTime(‘2019-01-21’); $sat_end = new DateTime(‘2019-12-30’); $sat_end = $sat_end->modify(‘+4 day’); $sat_interval = new DateInterval(‘P1D’); $sat_daterange = new DatePeriod($sat_begin, $sat_interval, $sat_end); $sun_begin = new DateTime(‘2019-01-22’); $sun_end = new DateTime(‘2019-12-31’); $sun_end = $sun_end->modify(‘+4 day’); $sun_interval = new DateInterval(‘P1D’); $sun_daterange = new DatePeriod($sun_begin, $sun_interval, $sun_end); foreach ($sat_daterange as $sat_date){

      $saturday = date('w', strtotime($sat_date->format('Y-m-d')));
        if ($saturday == 6 && $datei == $sat_date) {
      } 
  }foreach ($sun_daterange as $sun_date) {
     $sunday = date('w', strtotime($sun_date->format('Y-m-d')));
       if ($sunday == 0 && $datei == $sun_date) {
       }
     }
    switch ('Y-m-d') {
     case '2019-01-01':
     echo 'HAPPY NEW YEAR today is Tuesday 2019-01-01. A Public Holiday';
      break;
      case '2019-02-14':
      echo "HAPPY VALENTINE'S DAY , today is Thursday 2019-02-14 an observed 
       day, but NOT A PUBLIC HOLIDAY ";
       break;
      case '2019-03-08':
      echo "HAPPY WOMEN'S DAY , today is Friday 2019-03-08 an observed day to 
       recognize women, but NOT A PUBLIC HOLIDAY ";
       break;
       case $sat_date->format('Y-m-d'):
        echo 'HAPPY WEEKEND, today is '.'<b>'.'Saturday '.$sat_date->format("Y-m-d").'</b>'.'<br>' ;
        break;
      case $sun_date->format("Y-m-d"):
        echo 'HAPPY WEEKEND, today is '.'<b>'.'Sunday '.$sun_date->format("Y-m-d").'</b>'.'<br>' ;
        break;  
     default:
      echo "TODAY IS A WORKDAY, have a good day!".'<br>';
      break;
```

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I got this answer the next day, I asked the question on a forum. The answer was short, concise, precise and effective. I thought to share as it will be of help not to only this problem, but varieties of problems by tweaking the code. Credit to astonecipher

<?php
$holidays = [
    "01-01" => "New Years Day",
    "02-14" => "VALENTINE'S DAY, an observed day, but NOT A PUBLIC HOLIDAY",
    "03-08" => "WOMEN'S DAY",
    "12-25" => "Christmas Day",
    ];


$date = new DateTime();
for($index = 0; $index < 356; $index++)
{
    $date->modify('+1 day');
    if(array_key_exists($date->format("m-d"), $holidays))
        echo "Happy {$holidays[$date->format("m-d")]} {$date->format("Y-m-d")}";
    else if($date->format("w") > 0 && $date->format("w") < 6)
        echo "Today is a work day {$date->format("Y-m-d")}";
    else 
        echo "It's the weekend {$date->format("Y-m-d")}";

    echo "\n";
}
?>