Print correct dates in PHP

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
TeeOgunji
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Feb 08, 2019 1:27 am

Fri Feb 08, 2019 1:38 am

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:

Code: Select all

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

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 839
Joined: Mon Feb 22, 2016 5:52 pm

Fri Feb 08, 2019 9:28 am

You have quite a few problems in that code. I can't work out how any of it works as the switch statement can only have one result?

Your foreach loops don't do anything if they find a weekend day.

I suggest that you re-think how you want to achieve the end result as the way you are going about it can only lead to hard work.

Fixed dates are stuck in one year.
You're checking dates that will never happen again.
The loops through a date range is excessive just to find a weekend date, and you do this for both Saturday and Sunday? You already use date('w') to find the day of the week (hint).
The numbers of calls to format the date is excessive and pointless.

TeeOgunji
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Feb 08, 2019 1:27 am

Sat Feb 09, 2019 6:20 am

@hyper, let's take it one after the other, your 1st observation: You are right, I only want one result per day, that is why I am using the switch statement, "switch" happens to be effective than several 'if, else if" statements considering size/length of iterations, though this is my view.
2nd observation: Yeah, for each loop don't do anything when they find a weekend...and that is the reason why I came on this platform to seek assistance.
3rd & 4th obs: Yeah, fixed dates are stuck in one year because these dates fall on different days of the week in a different year, they will never happen on the same day for another year, that is why I made it fixed.
If you have a shorter code that answers my question, I am ready to accept your answer.
I want a result that will print a single date for a single day (A date for A day). For example, in my part of the world, today is Saturday 9th, Feb. 2019. I want my result to be: Happy Weekend, today is Saturday 2019-02-09 only. Tomorrow is Sun.10th, Feb. I want it to print: Happy Weekend, today is Sunday 2019-02-10 only and so on for Mon-Fri. Not all dates at a go(at once). Thanks, pal.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 839
Joined: Mon Feb 22, 2016 5:52 pm

Sat Feb 09, 2019 6:58 am

I don't know what you're expecting, are you trying to learn PHP and want some help?
the switch statement, "switch" happens to be effective than several 'if, else if"
Correct, but irrelevant, there are more efficient and user friendly ways that don't involve writing pages of variables, ifs or cases.
Yeah, for each loop don't do anything when they find a weekend...and that is the reason why I came on this platform to seek assistance.
There is your answer to the original post?

Code: Select all

foreach ($sat_daterange as $sat_date){

          $saturday = date('w', strtotime($sat_date->format('Y-m-d')));
            if ($saturday == 6 && $datei == $sat_date) {
            # Nothing doing here
          } 
      }
What did you expect it to do?
3rd & 4th obs: Yeah, fixed dates are stuck in one year because these dates fall on different days of the week in a different year, they will never happen on the same day for another year, that is why I made it fixed.
That seems a very short term objective, most programs are written to be as useful as possible, for instance: why don't you use the month and day instead of the year to show anniversaries? e.g. use 03-17 for St Patricks day which is every year.
For example, in my part of the world, today is Saturday 9th, Feb. 2019.
So check today and see if it's a weekend day rather than generate a list of all dates and loop through them?

Have you actually looked at the date function?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 839
Joined: Mon Feb 22, 2016 5:52 pm

Sat Feb 09, 2019 11:19 am

I meant to state on my last post:

Well done for giving it a go!

TeeOgunji
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Feb 08, 2019 1:27 am

Sat Feb 09, 2019 2:46 pm

Yea, I had looked at date function. But I still see that you don't really understand my quest. The code I am working on is going to loop through the year and print out the result of the code that matches the date in the present year. I am here for solutions and if you know how best to get it done, please share. I had earlier executed an "echo" command with the for each loop and didn't get a satisfying result. I have stated here not once, that getting Saturday's date on a Saturday or Sunday's date on a Sunday brought me to the forum. Other days of the week works fine adding weekends to it is the issue. Do take good note of these examples of dates that change every year: Easter for Christians and Id el Kabir for Muslims just to share some.

TeeOgunji
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Feb 08, 2019 1:27 am

Sat Feb 09, 2019 3:40 pm

Thanks, I got an answer from another forum that is working fine now, but that should not debar you from sharing your solution if you got one. I am open to better ways of doing things.

TeeOgunji
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Feb 08, 2019 1:27 am

Tue Feb 12, 2019 2:05 am

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

Code: Select all

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

Post Reply