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;
    ```

You are making this too complicated.

1 Like
<?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";
}

Output:

It’s the weekend 2019-02-09
It’s the weekend 2019-02-10
Today is a work day 2019-02-11
Today is a work day 2019-02-12
Today is a work day 2019-02-13
Happy VALENTINE’S DAY, an observed day, but NOT A PUBLIC HOLIDAY 2019-02-14
Today is a work day 2019-02-15
It’s the weekend 2019-02-16
It’s the weekend 2019-02-17
Today is a work day 2019-02-18
Today is a work day 2019-02-19
Today is a work day 2019-02-20
Today is a work day 2019-02-21
Today is a work day 2019-02-22
It’s the weekend 2019-02-23
It’s the weekend 2019-02-24
Today is a work day 2019-02-25
Today is a work day 2019-02-26
Today is a work day 2019-02-27
Today is a work day 2019-02-28
Today is a work day 2019-03-01
It’s the weekend 2019-03-02
It’s the weekend 2019-03-03
Today is a work day 2019-03-04
Today is a work day 2019-03-05
Today is a work day 2019-03-06
Today is a work day 2019-03-07
Happy WOMEN’S DAY 2019-03-08
It’s the weekend 2019-03-09
It’s the weekend 2019-03-10
Today is a work day 2019-03-11
Today is a work day 2019-03-12
Today is a work day 2019-03-13
Today is a work day 2019-03-14
Today is a work day 2019-03-15
It’s the weekend 2019-03-16
It’s the weekend 2019-03-17
Today is a work day 2019-03-18
Today is a work day 2019-03-19
Today is a work day 2019-03-20
Today is a work day 2019-03-21
Today is a work day 2019-03-22
It’s the weekend 2019-03-23
It’s the weekend 2019-03-24
Today is a work day 2019-03-25
Today is a work day 2019-03-26
Today is a work day 2019-03-27
Today is a work day 2019-03-28
Today is a work day 2019-03-29
It’s the weekend 2019-03-30
It’s the weekend 2019-03-31
Today is a work day 2019-04-01
Today is a work day 2019-04-02
Today is a work day 2019-04-03
Today is a work day 2019-04-04
Today is a work day 2019-04-05
It’s the weekend 2019-04-06
It’s the weekend 2019-04-07
Today is a work day 2019-04-08
Today is a work day 2019-04-09
Today is a work day 2019-04-10
Today is a work day 2019-04-11
Today is a work day 2019-04-12
It’s the weekend 2019-04-13
It’s the weekend 2019-04-14
Today is a work day 2019-04-15
Today is a work day 2019-04-16
Today is a work day 2019-04-17
Today is a work day 2019-04-18
Today is a work day 2019-04-19
It’s the weekend 2019-04-20
It’s the weekend 2019-04-21
Today is a work day 2019-04-22
Today is a work day 2019-04-23
Today is a work day 2019-04-24
Today is a work day 2019-04-25
Today is a work day 2019-04-26
It’s the weekend 2019-04-27
It’s the weekend 2019-04-28
Today is a work day 2019-04-29
Today is a work day 2019-04-30
Today is a work day 2019-05-01
Today is a work day 2019-05-02
Today is a work day 2019-05-03
It’s the weekend 2019-05-04
It’s the weekend 2019-05-05
Today is a work day 2019-05-06
Today is a work day 2019-05-07
Today is a work day 2019-05-08
Today is a work day 2019-05-09
Today is a work day 2019-05-10
It’s the weekend 2019-05-11
It’s the weekend 2019-05-12
Today is a work day 2019-05-13
Today is a work day 2019-05-14
Today is a work day 2019-05-15
Today is a work day 2019-05-16
Today is a work day 2019-05-17
It’s the weekend 2019-05-18
It’s the weekend 2019-05-19
Today is a work day 2019-05-20
Today is a work day 2019-05-21
Today is a work day 2019-05-22
Today is a work day 2019-05-23
Today is a work day 2019-05-24
It’s the weekend 2019-05-25
It’s the weekend 2019-05-26
Today is a work day 2019-05-27
Today is a work day 2019-05-28
Today is a work day 2019-05-29
Today is a work day 2019-05-30
Today is a work day 2019-05-31
It’s the weekend 2019-06-01
It’s the weekend 2019-06-02
Today is a work day 2019-06-03
Today is a work day 2019-06-04
Today is a work day 2019-06-05
Today is a work day 2019-06-06
Today is a work day 2019-06-07
It’s the weekend 2019-06-08
It’s the weekend 2019-06-09
Today is a work day 2019-06-10
Today is a work day 2019-06-11
Today is a work day 2019-06-12
Today is a work day 2019-06-13
Today is a work day 2019-06-14
It’s the weekend 2019-06-15
It’s the weekend 2019-06-16
Today is a work day 2019-06-17
Today is a work day 2019-06-18
Today is a work day 2019-06-19
Today is a work day 2019-06-20
Today is a work day 2019-06-21
It’s the weekend 2019-06-22
It’s the weekend 2019-06-23
Today is a work day 2019-06-24
Today is a work day 2019-06-25
Today is a work day 2019-06-26
Today is a work day 2019-06-27
Today is a work day 2019-06-28
It’s the weekend 2019-06-29
It’s the weekend 2019-06-30
Today is a work day 2019-07-01
Today is a work day 2019-07-02
Today is a work day 2019-07-03
Today is a work day 2019-07-04
Today is a work day 2019-07-05
It’s the weekend 2019-07-06
It’s the weekend 2019-07-07
Today is a work day 2019-07-08
Today is a work day 2019-07-09
Today is a work day 2019-07-10
Today is a work day 2019-07-11
Today is a work day 2019-07-12
It’s the weekend 2019-07-13
It’s the weekend 2019-07-14
Today is a work day 2019-07-15
Today is a work day 2019-07-16
Today is a work day 2019-07-17
Today is a work day 2019-07-18
Today is a work day 2019-07-19
It’s the weekend 2019-07-20
It’s the weekend 2019-07-21
Today is a work day 2019-07-22
Today is a work day 2019-07-23
Today is a work day 2019-07-24
Today is a work day 2019-07-25
Today is a work day 2019-07-26
It’s the weekend 2019-07-27
It’s the weekend 2019-07-28
Today is a work day 2019-07-29
Today is a work day 2019-07-30
Today is a work day 2019-07-31
Today is a work day 2019-08-01
Today is a work day 2019-08-02
It’s the weekend 2019-08-03
It’s the weekend 2019-08-04
Today is a work day 2019-08-05
Today is a work day 2019-08-06
Today is a work day 2019-08-07
Today is a work day 2019-08-08
Today is a work day 2019-08-09
It’s the weekend 2019-08-10
It’s the weekend 2019-08-11
Today is a work day 2019-08-12
Today is a work day 2019-08-13
Today is a work day 2019-08-14
Today is a work day 2019-08-15
Today is a work day 2019-08-16
It’s the weekend 2019-08-17
It’s the weekend 2019-08-18
Today is a work day 2019-08-19
Today is a work day 2019-08-20
Today is a work day 2019-08-21
Today is a work day 2019-08-22
Today is a work day 2019-08-23
It’s the weekend 2019-08-24
It’s the weekend 2019-08-25
Today is a work day 2019-08-26
Today is a work day 2019-08-27
Today is a work day 2019-08-28
Today is a work day 2019-08-29
Today is a work day 2019-08-30
It’s the weekend 2019-08-31
It’s the weekend 2019-09-01
Today is a work day 2019-09-02
Today is a work day 2019-09-03
Today is a work day 2019-09-04
Today is a work day 2019-09-05
Today is a work day 2019-09-06
It’s the weekend 2019-09-07
It’s the weekend 2019-09-08
Today is a work day 2019-09-09
Today is a work day 2019-09-10
Today is a work day 2019-09-11
Today is a work day 2019-09-12
Today is a work day 2019-09-13
It’s the weekend 2019-09-14
It’s the weekend 2019-09-15
Today is a work day 2019-09-16
Today is a work day 2019-09-17
Today is a work day 2019-09-18
Today is a work day 2019-09-19
Today is a work day 2019-09-20
It’s the weekend 2019-09-21
It’s the weekend 2019-09-22
Today is a work day 2019-09-23
Today is a work day 2019-09-24
Today is a work day 2019-09-25
Today is a work day 2019-09-26
Today is a work day 2019-09-27
It’s the weekend 2019-09-28
It’s the weekend 2019-09-29
Today is a work day 2019-09-30
Today is a work day 2019-10-01
Today is a work day 2019-10-02
Today is a work day 2019-10-03
Today is a work day 2019-10-04
It’s the weekend 2019-10-05
It’s the weekend 2019-10-06
Today is a work day 2019-10-07
Today is a work day 2019-10-08
Today is a work day 2019-10-09
Today is a work day 2019-10-10
Today is a work day 2019-10-11
It’s the weekend 2019-10-12
It’s the weekend 2019-10-13
Today is a work day 2019-10-14
Today is a work day 2019-10-15
Today is a work day 2019-10-16
Today is a work day 2019-10-17
Today is a work day 2019-10-18
It’s the weekend 2019-10-19
It’s the weekend 2019-10-20
Today is a work day 2019-10-21
Today is a work day 2019-10-22
Today is a work day 2019-10-23
Today is a work day 2019-10-24
Today is a work day 2019-10-25
It’s the weekend 2019-10-26
It’s the weekend 2019-10-27
Today is a work day 2019-10-28
Today is a work day 2019-10-29
Today is a work day 2019-10-30
Today is a work day 2019-10-31
Today is a work day 2019-11-01
It’s the weekend 2019-11-02
It’s the weekend 2019-11-03
Today is a work day 2019-11-04
Today is a work day 2019-11-05
Today is a work day 2019-11-06
Today is a work day 2019-11-07
Today is a work day 2019-11-08
It’s the weekend 2019-11-09
It’s the weekend 2019-11-10
Today is a work day 2019-11-11
Today is a work day 2019-11-12
Today is a work day 2019-11-13
Today is a work day 2019-11-14
Today is a work day 2019-11-15
It’s the weekend 2019-11-16
It’s the weekend 2019-11-17
Today is a work day 2019-11-18
Today is a work day 2019-11-19
Today is a work day 2019-11-20
Today is a work day 2019-11-21
Today is a work day 2019-11-22
It’s the weekend 2019-11-23
It’s the weekend 2019-11-24
Today is a work day 2019-11-25
Today is a work day 2019-11-26
Today is a work day 2019-11-27
Today is a work day 2019-11-28
Today is a work day 2019-11-29
It’s the weekend 2019-11-30
It’s the weekend 2019-12-01
Today is a work day 2019-12-02
Today is a work day 2019-12-03
Today is a work day 2019-12-04
Today is a work day 2019-12-05
Today is a work day 2019-12-06
It’s the weekend 2019-12-07
It’s the weekend 2019-12-08
Today is a work day 2019-12-09
Today is a work day 2019-12-10
Today is a work day 2019-12-11
Today is a work day 2019-12-12
Today is a work day 2019-12-13
It’s the weekend 2019-12-14
It’s the weekend 2019-12-15
Today is a work day 2019-12-16
Today is a work day 2019-12-17
Today is a work day 2019-12-18
Today is a work day 2019-12-19
Today is a work day 2019-12-20
It’s the weekend 2019-12-21
It’s the weekend 2019-12-22
Today is a work day 2019-12-23
Today is a work day 2019-12-24
Happy Christmas Day 2019-12-25
Today is a work day 2019-12-26
Today is a work day 2019-12-27
It’s the weekend 2019-12-28
It’s the weekend 2019-12-29
Today is a work day 2019-12-30
Today is a work day 2019-12-31
Happy New Years Day 2020-01-01
Today is a work day 2020-01-02
Today is a work day 2020-01-03
It’s the weekend 2020-01-04
It’s the weekend 2020-01-05
Today is a work day 2020-01-06
Today is a work day 2020-01-07
Today is a work day 2020-01-08
Today is a work day 2020-01-09
Today is a work day 2020-01-10
It’s the weekend 2020-01-11
It’s the weekend 2020-01-12
Today is a work day 2020-01-13
Today is a work day 2020-01-14
Today is a work day 2020-01-15
Today is a work day 2020-01-16
Today is a work day 2020-01-17
It’s the weekend 2020-01-18
It’s the weekend 2020-01-19
Today is a work day 2020-01-20
Today is a work day 2020-01-21
Today is a work day 2020-01-22
Today is a work day 2020-01-23
Today is a work day 2020-01-24
It’s the weekend 2020-01-25
It’s the weekend 2020-01-26
Today is a work day 2020-01-27
Today is a work day 2020-01-28
Today is a work day 2020-01-29
Today is a work day 2020-01-30

1 Like

Your code is short and precise, I love it but it doesn’t answer my question. I want to print a date on a day. Not All Dates At Once. Each day’s date is what I want. I am able to do so for all other days except the weekends, so I want a solution for weekends. In my part of the world now, it is Saturday the 9th of February 2019. I want to print only: Happy Weekend, today is Saturday 2019-02-09 only. Not all the dates in the year at a go. Tomorrow is Sunday the 10th. I want to print: Happy Weekend, today is Sunday 2019-02-10 only and so on like that. Not all dates at a go(at once). Thanks, pal.

You aren’t understanding what I was showing, and I’m not your pal. You don’t need to do a foreach loop to get all the weekends or anything else, just print the current day and format it, it hits the mapper in the array for holidays to see if the date is special. If you remove the loop, it will print, for today… You just need to format the date to get what you want, like the name of the day.

Please can you do that and post, it will not only help me, there are millions of people that will benefit from that, even if you ain’t-a pal. As you don’t need to be to help, I am certain. Thanks for helping out, your codes are short and precise and working. Hope to learn more from you later.

Take the code I posted an play with it.

I fail to see how day being printed will help anyone.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service