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(”);
$newYeardate = $newYear->format('2019-01-01’);
$ValentineS = new DateTime(“);
$valentineSdate = $ValentineS->format('2019-02-14’);
$WomenDay = new DateTime(”);
$WomenDaydate = $WomenDay->format('2019-03-08’);
/* …other days not shown for brevity../
/…move to the weekends....*/
$satbegin = new DateTime('2019-01-21’);
$satend = new DateTime('2019-12-30’);
$satend = $satend->modify(’+4 day’);
$satinterval = new DateInterval('P1D’);
$satdaterange = new DatePeriod($satbegin, $satinterval, $satend);
$sunbegin = new DateTime('2019-01-22’);
$sunend = new DateTime('2019-12-31’);
$sunend = $sunend->modify(’+4 day’);
$suninterval = new DateInterval('P1D’);
$sundaterange = new DatePeriod($sunbegin, $suninterval, $sunend);
foreach ($satdaterange 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;
```
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.
×