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

URL参数日期字符串经strtotime()转时间戳失败,如何解决?

解决方法

你的问题出在URL参数里的%20是URL编码后的空格字符,strtotime()无法识别这种编码格式的分隔符,导致转换失败。

解决步骤很简单,先对URL参数进行解码,再传给strtotime():

$urlParameter = '2022-09-30%2010:36:13';
// 先解码URL参数
$decodedDate = urldecode($urlParameter);
$timestamp = strtotime($decodedDate);

echo $timestamp;
// 输出:1664510173

也可以直接用str_replace()替换%20为空格,但urldecode()更通用,能处理其他可能的URL编码字符(比如%2F等):

$urlParameter = '2022-09-30%2010:36:13';
$fixedDate = str_replace('%20', ' ', $urlParameter);
$timestamp = strtotime($fixedDate);

echo $timestamp;
// 输出:1664510173

另外,如果你是直接从$_GET或$_REQUEST获取参数,PHP会自动帮你解码,不需要手动处理。比如如果参数是通过?date=2022-09-30%2010:36:13传递的,直接用$_GET['date']就能拿到解码后的字符串,直接传给strtotime()即可。

内容的提问来源于stack exchange,提问作者owf

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 00:10:51