date()方法,“遇到的非良好的数值”不想格式化在$ _POST中传递的日期 [英] date() method, "A non well formed numeric value encountered" does not want to format a date passed in $_POST

查看:257
本文介绍了date()方法,“遇到的非良好的数值”不想格式化在$ _POST中传递的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有问题的行:

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

抛出以下错误:

 注意:一个非格式的数值遇到

所以我var var以确保格式很好。

  var_dump($ _ POST ['nextAppointmentDate']); 

string(10)12-16-2013​​

一个href =http://www.php.net/manual/en/function.date.php> php docs状态它需要一个不是字符串的时间戳。但是当我这样做时:

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

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

  string(10)1969-12-31
pre>

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



谢谢!

解决方案

strtotime()


日期通过查看各个组件之间的分隔符来消除m / d / y或dmy格式的错误:如果分隔符是斜杠(/),则假定为美国m / d / y;而如果分隔符是破折号( - )或点(。),则假定为欧洲dmy格式。


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



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

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


I unfortunately can't use DateTime() as the server this project is on is running PHP v.5.2.

the line in question:

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

throws the following error:

Notice: A non well formed numeric value encountered

so I var dump to make sure it's well formatted..

var_dump($_POST['nextAppointmentDate']);  

string(10) "12-16-2013"

The php docs state that it takes a timestamp not a string. but when I do:

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

and then var_dump the result, I get this:

string(10) "1969-12-31"

why can I not format a date with this date value and strtotime()?

thanks!

解决方案

From the documentation for 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.

In your date string, you have 12-16-2013. 16 isn't a valid month, and hence strtotime() returns false.

Since you can't use DateTime class, you could manually replace the - with / using str_replace() to convert the date string into a format that strtotime() understands:

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

这篇关于date()方法,“遇到的非良好的数值”不想格式化在$ _POST中传递的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆