- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
很遗憾,我无法使用 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-2013
。 16
不是有效月份,因此 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/
我是一名优秀的程序员,十分优秀!