You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP报错Call to undefined function cal_days_in_month()求解决方案

解决PHP报错:Call to undefined function cal_days_in_month()

报错原因

cal_days_in_month() 是PHP Calendar扩展提供的函数,你的运行环境未启用该扩展,因此触发了“未定义函数”的错误。

解决方案

方案1:启用Calendar扩展

根据服务器环境不同,操作步骤如下:

  • Linux(Ubuntu/Debian等):
    安装Calendar扩展后重启PHP服务:

    sudo apt-get install php-calendar
    sudo systemctl restart php-fpm # 若用Apache则重启apache2
    

    若为编译安装的PHP,需在编译时添加--enable-calendar参数,重新编译后生效。

  • Windows(WAMP/XAMPP等):
    打开PHP配置文件php.ini,找到;extension=calendar这一行,去掉前面的分号;,保存后重启Web服务(Apache/Nginx)。

方案2:用内置函数替代(更简便)

其实你的代码里已经实现了获取当月天数的逻辑——date('t')对应的变量$get_dz_thismonth,它的功能就是返回指定月份的总天数,完全可以替代cal_days_in_month()。直接修改代码即可:

// 替换原cal_days_in_month的调用
$totaldays_of_thismonth = $get_dz_thismonth;
// 或者直接调用date函数,更简洁
$totaldays_of_thismonth = date('t', strtotime($currentdate));

另外,你的代码存在重复调用strtotime($currentdate)的情况,可以优化一下,把解析后的时间戳存到变量里复用,提升效率:

// 注意:原代码date('2023-07')写法错误,date第一个参数是格式,指定固定年月直接赋值字符串即可
$currentdate = '2023-07';
// 若要获取当前年月,改为$currentdate = date('Y-m');
$convertdate = strtotime($currentdate);

// Current Year, Month, Date
$get_yy_thismonth = date('Y', $convertdate);
$get_mf_thismonth = date('F', $convertdate);
$get_mm_thismonth = date('m', $convertdate);
$get_mn_thismonth = date('n', $convertdate);
$get_dt_thismonth = date('d', $convertdate);
$get_dy_thismonth = date('D', $convertdate);
$get_dz_thismonth = date('t', $convertdate);

$get_nn_thismonth = date('Y-m', $convertdate);
$totaldays_of_thismonth = $get_dz_thismonth;

内容的提问来源于stack exchange,提问作者Indo Grafis Depok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 13:32:13