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

标签 php date strtotime

很遗憾,我无法使用 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/

相关文章:

php - 如何让 PHP 执行停止,直到采取某些操作

r - 在一系列日期中识别第三个星期五

sql - 在oracle中选择无效的日期字符串

php - 在每个月的 "nth"工作日重复事件

php - $日期 + 1 年?

php & MySQL - 允许用户更新他们的详细信息

php - GD 和 php 5.3 中的字距调整问题

php - 登山扣问题 -- Codeigniter 库

php - 如何在 PHP 中将日期格式化为意大利语?

PHP date() 和 strtotime() 在 31 日返回错误的月份