我将整个代码放在这里。我想针对一年中的每个不同日期(天)打印出代码中显示的不同信息。从问我的第一天开始,我就一直在尝试不同的PHP函数,但似乎没有得到答案。一周的其他两天都很好,但是周末(星期六或日)一直令人头疼。请在周末打印正确的日期是需要更好的程序员回答的问题。(例如,让我们在本周和下周的周末进行拍摄,我应该打印以下内容:周末快乐,今天是 2019-02-星期六09 。在第二天,即星期日,我应该打印:快乐周末,今天是星期日2019-02-10 。同样,下周周末应该给2019-02-周末的不同日期分别为16日和2019-02-17,但这没有发生。我已经参加了将近1个月,甚至在2周前在这里提出问题之前,请帮忙。
$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;
答案 0 :(得分:0)
您必须将间隔代码放入循环范围内:
<?php
$date = new DateTime();
$datei = $date->format('Y-m-d');
$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){
$interval = $sat_date->diff($date);
$saturday = date('w', strtotime($sat_date->format("Y-m-d")));
if ($saturday == 6 || $datei == $sat_date) {
echo 'Happy Weekend, today is '. '<b> '.$sat_date->format("Y-m-d").' </b>' . '. We have' .$interval->format(' <b> %a days </b> to the Weekend.')."<br><br>";
}
}
foreach ($sun_daterange as $sun_date) {
$interval = $sun_date->diff($date);
$sunday = date('w', strtotime($sun_date->format("Y-m-d")));
if ($sunday == 0 || $datei == $sun_date) {
echo 'Happy Weekend, today is '. '<b> '.$sun_date->format("Y-m-d").' </b>' . '. We have' .$interval->format(' <b> %a days </b> to the Weekend.')."<br>";
}
}
?>
答案 1 :(得分:0)
第二天,我得到了这个答案,我在一个论坛上问了这个问题。答案是简短,简洁,准确和有效的。我想分享一下,因为通过调整代码不仅对这个问题有帮助,而且对各种各样的问题也有帮助。归功于 密码 。
<?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";
}
?>
答案 2 :(得分:-2)
欢迎来到Stackoverflow!
这是我为您提供的解决方案:
$currentdate = getdate();
if ($currentdate['wday'] == 1) {
$days = 5;
} elseif ($currentdate['wday'] == 2) {
$days = 3;
} elseif ($currentdate['wday'] == 3) {
$days = 2;
} elseif ($currentdate['wday'] == 4) {
$days = 1;
} elseif ($currentdate['wday'] == 5) {
$days = 0;
} elseif ($currentdate['wday'] == 6) {
$days = 0;
} elseif ($currentdate['wday'] == 0) {
$days = 0;
}
$date = $currentdate['year'].'-'.$currentdate['mon'].'-'.$currentdate['mday'];
$weekenddate = date('Y-m-d', strtotime($date. ' + '.$days.' days'));
echo 'Happy Weekend, Today is '.$date.'. We have '.$days.' day(s) to the Weekends';
我刚刚测试过,距离周末还有1天:P
我想开心吗?