Print correct dates in PHP

February 7, 2019 360 views
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(”);
$newYeardate = $newYear->format('2019-01-01’);
$ValentineS = new DateTime(“);
$valentineS
date = $ValentineS->format('2019-02-14’);
$WomenDay = new DateTime(”);
$WomenDaydate = $WomenDay->format('2019-03-08’);
/* …other days not shown for brevity../
/
…move to the weekends....*/
$sat
begin = new DateTime('2019-01-21’);
$satend = new DateTime('2019-12-30’);
$sat
end = $satend->modify(’+4 day’);
$sat
interval = new DateInterval('P1D’);
$satdaterange = new DatePeriod($satbegin, $satinterval, $satend);
$sunbegin = new DateTime('2019-01-22’);
$sun
end = new DateTime('2019-12-31’);
$sunend = $sunend->modify(’+4 day’);
$suninterval = new DateInterval('P1D’);
$sun
daterange = new DatePeriod($sunbegin, $suninterval, $sunend);
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;
```
1 Answer

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";
}
?>
Have another answer? Share your knowledge.