gpt4 book ai didi

php - date() 方法, "A non well formed numeric value encountered"不想格式化 $_POST 中传递的日期

转载 作者:IT王子 更新时间:2023-10-28 23:51:15 27 4
gpt4 key购买 nike

很遗憾,我无法使用 DateTime(),因为该项目所在的服务器正在运行 PHP v.5.2。

有问题的行:

$aptnDate2 = date('Y-m-d', $_POST['nextAppointmentDate']); 

抛出以下错误:

Notice: A non well formed numeric value encountered

所以我 var dump 以确保它的格式正确..

var_dump($_POST['nextAppointmentDate']);  

string(10) "12-16-2013"

php docs state它需要一个时间戳而不是一个字符串。但是当我这样做时:

date('Y-m-d', strtotime($_POST['nextAppointmentDate']));

然后 var_dump 结果,我得到这个:

string(10) "1969-12-31"

为什么我不能用这个日期值和 strtotime() 格式化日期?

谢谢!

最佳答案

来自 strtotime() 的文档:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

在您的日期字符串中,您有 12-16-201316 不是有效月份,因此 strtotime() 返回 false

由于您不能使用 DateTime 类,您可以使用 str_replace() 手动将 - 替换为 /将日期字符串转换为 strtotime() 可以理解的格式:

$date = '2-16-2013';
echo date('Y-m-d', strtotime(str_replace('-','/', $date))); // => 2013-02-16

关于php - date() 方法, "A non well formed numeric value encountered"不想格式化 $_POST 中传递的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20574465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com