Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 日期()方法;遇到格式不正确的数值“quot;不想格式化在$\u POST中传递的日期_Php_Date_Strtotime - Fatal编程技术网

Php 日期()方法;遇到格式不正确的数值“quot;不想格式化在$\u POST中传递的日期

Php 日期()方法;遇到格式不正确的数值“quot;不想格式化在$\u POST中传递的日期,php,date,strtotime,Php,Date,Strtotime,很遗憾,我不能使用DateTime(),因为该项目所在的服务器正在运行PHPV5.2 所涉行: $aptnDate2 = date('Y-m-d', $_POST['nextAppointmentDate']); 引发以下错误: Notice: A non well formed numeric value encountered 因此,我需要转储文件以确保其格式正确 var_dump($_POST['nextAppointmentDate']); string(10) "12-16

很遗憾,我不能使用
DateTime()
,因为该项目所在的服务器正在运行PHPV5.2

所涉行:

$aptnDate2 = date('Y-m-d', $_POST['nextAppointmentDate']); 
引发以下错误:

Notice: A non well formed numeric value encountered
因此,我需要转储文件以确保其格式正确

var_dump($_POST['nextAppointmentDate']);  

string(10) "12-16-2013"
结果表明,它采用的是时间戳,而不是字符串。但当我这样做的时候:

date('Y-m-d', strtotime($_POST['nextAppointmentDate']));
然后
var\u dump
结果是:

string(10) "1969-12-31"
为什么不能使用此日期值和strotime()格式化日期?


谢谢

来自以下文件:

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

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

由于无法使用DateTime类,因此可以手动将
-
替换为
/
以将日期字符串转换为
strotime()
可以理解的格式:

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

干杯,伙计,就这样。我可以发誓破折号是可以接受的,也许这只是因为
DateTime()
?谢谢你的帮助。:)@前缀:如果将格式不正确的日期字符串传递给
DateTime
,它(通常)会引发异常。有关可接受日期格式的列表,请参见。如果您使用的是DateTime,那么
DateTime::createFromFormat()
就是一种方法: