微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-date()方法,“遇到的非格式数值”不想格式化在$_POST中传递的日期

我不幸的是不能使用DateTime()作为这个项目正在运行 PHP v.5.2的服务器.

有问题的行:

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

引发以下错误

Notice: A non well formed numeric value encountered

所以我的var转储,以确保它格式很好..

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类,您可以手动替换 – with / using str_replace()将日期字符串转换为strtotime()了解的格式:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐