我有这个代码:
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;
}
当我删除将我的数字$month从参数转换为带有 strftime("%b",$date)的字符串的行时,它给出了良好的行为。当我添加这一行时,var $day在每月的第一天开始重复 9 次......这是星期二,无法得到解决方案,这对我来说是一个错误......
您要替换在以下位置中使用的变量:
$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;
}
替换代码
$date = strtotime($year . "-" . $month . "-" . ($i + 1));
到
$date = strtotime($year . "-" . $month . "-" .str_pad(($i + 1), 2, '0', STR_PAD_LEFT) );
我的去!!非常感谢你们,几天来我一直在寻找我做错了什么......
只是一个变量名称与我的数字$month冲突,字符串月份与 strftime 冲突。所以我替换了字符串的名称。
大错特错! ^^