我有以下代码:
function calendarDay ($month, $year)
{
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$today = strtotime(date("Y-m-d"));
$result = array();
$str = "";
$strMonth = "";
for ($i = 0; $i < $num; $i++) {
$date = strtotime($year . "-" . $month . "-" . ($i + 1));
$day=strftime("%a", $date);
$month = strftime("%b", $date);
if ($today == $date) {
if ($day == "Sat" || $day == "Sun") {
$str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
$strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $month." ". "</th>";
}
else {
$str .= "<th style='background-color:#888888'>" . $day. "</th>";
$strMonth = $strMonth . "<th style='background-color:#888888'>".($i + 1) . "-" . $month." ". "</th>";
}
}
else if ($today != $date) {
if ($day == "Sat" || $day == "Sun") {
$str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
$strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $month." ". "</th>";
}
else {
$str .= "<th>" . $day. "</th>";
$strMonth = $strMonth . "<th>".($i + 1) . "-" . $month." ". "</th>";
}
}
$result = array_merge($result, array("Month" => $strMonth));
$result = array_merge($result, array("Day" => $str));
}
return $result;
}
当我删除使用strftime(“%b”,$ date)将数字$ month从参数转换为字符串的行时,它表现出良好的行为。 当我添加这行代码时,var $ day开始在每月的第一天重复9次...直到星期二,并且无法获得解决方案,这对我来说是个错误...
答案 0 :(得分:0)
您要替换在以下位置使用的变量:
$date = strtotime($year . "-" . $month . "-" . ($i + 1));
因此,在每月的第二天,不是解析2019-1-2
,而是解析2019-Jan-1
。我不确定为什么,但是当您将此格式与一位数字的日期结合使用时,它总是像2019-01-01
一样进行解析。
解决方案是使用其他变量。
function calendarDay ($month, $year)
{
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$today = strtotime(date("Y-m-d"));
$result = array();
$str = "";
$strMonth = "";
for ($i = 0; $i < $num; $i++) {
$date = strtotime($year . "-" . $month . "-" . ($i + 1));
$day=strftime("%a", $date);
$monthName = strftime("%b", $date);
if ($today == $date) {
if ($day == "Sat" || $day == "Sun") {
$str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
$strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $monthName." ". "</th>";
}
else {
$str .= "<th style='background-color:#888888'>" . $day. "</th>";
$strMonth = $strMonth . "<th style='background-color:#888888'>".($i + 1) . "-" . $monthName." ". "</th>";
}
}
else if ($today != $date) {
if ($day == "Sat" || $day == "Sun") {
$str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
$strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $monthName." ". "</th>";
}
else {
$str .= "<th>" . $day. "</th>";
$strMonth = $strMonth . "<th>".($i + 1) . "-" . $monthName." ". "</th>";
}
}
$result = array_merge($result, array("Month" => $strMonth));
$result = array_merge($result, array("Day" => $str));
}
return $result;
}
答案 1 :(得分:0)
替换代码
$date = strtotime($year . "-" . $month . "-" . ($i + 1));
进入
$date = strtotime($year . "-" . $month . "-" .str_pad(($i + 1), 2, '0', STR_PAD_LEFT) );
答案 2 :(得分:0)
哦,我走了,谢谢大家!!!很多天以来,我一直在搜索自己在做错什么...
一个变量名与我的数字$ month和带strftime的字符串month冲突。所以我将字符串的名称替换为一个。
严重错误! ^^