
php 升级后 `mktime()` 不再接受字符串参数,必须传入整型的时分秒等数值;原代码将格式化时间字符串直接传入,触发 typeerror,需改用 `time()` 或正确拆解日期组件。
在迁移至 Plesk 服务器(通常预装 PHP 8.0+)后,您遇到的报错:
Fatal error: Uncaught TypeError: mktime(): Argument #1 ($hour) must be of type int, string given
根本原因在于:PHP 8.0 起对内置函数参数类型执行严格校验。mktime() 的第一个参数 $hour 明确要求 int 类型,而您的代码中:
$ah = date("H:i:s Y-m-d"); // 输出类似:"14:23:59 2024-06-15"
$ahseg = mktime($ah); // ❌ 错误:把整个字符串传给了 $hour 参数$ah 是一个格式化后的字符串(如 "14:23:59 2024-06-15"),而非整数,因此直接传入 mktime() 违反类型约束,PHP 8+ 拒绝静默转换并抛出 TypeError——这正是旧版 PHP(如 7.4 及更早)允许的“松散类型”行为被彻底移除的结果。
✅ 正确解决方案如下:
立即学习“PHP免费学习笔记(深入)”;
✅ 方案一:若只需当前时间戳 → 直接使用 time()
最简洁、高效且语义清晰:
$ahseg = time(); // 返回当前 Unix 时间戳(int),无需解析
✅ 方案二:若需基于特定日期时间生成时间戳 → 使用 mktime() 的正确方式
必须分别传入 int 类型的年、月、日、时、分、秒:
// 先获取各组件(注意:date() 返回字符串,需 (int) 强转)
$now = getdate();
$ahseg = mktime(
(int)$now['hours'], // hour
(int)$now['minutes'], // minute
(int)$now['seconds'], // second
(int)$now['mon'], // month (1–12)
(int)$now['mday'], // day (1–31)
(int)$now['year'] // year (e.g., 2024)
);或更现代、推荐的方式:使用 DateTime 类(PHP 5.2+,类型安全、可读性强):
$dt = new DateTime(); // 当前时间
// 或指定格式字符串:new DateTime('2024-06-15 14:23:59');
$ahseg = $dt->getTimestamp(); // 返回 int 时间戳⚠️ 注意事项:
- 不要依赖 date() 字符串直接喂给 mktime() —— 它们语义和结构完全不同;
- Plesk 环境默认启用较新 PHP 版本(常为 8.0/8.1/8.2),务必检查 phpinfo() 或运行 php -v 确认版本;
- 开发阶段建议开启 declare(strict_types=1); 并配合静态分析工具(如 PHPStan),提前捕获类型问题。
总结:该错误不是 Plesk 特有问题,而是 PHP 语言演进带来的兼容性升级。修复核心在于理解 mktime() 的参数契约,并优先选用语义明确、类型安全的替代方案(如 time() 或 DateTime)。











