php 警告:date() 期望参数 2 很长,字符串在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5254553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: date() expects parameter 2 to be long, string given in
提问by methuselah
i keep getting this error on the car_detail.php page on my database
我在数据库的 car_detail.php 页面上不断收到此错误
Warning: date() expects parameter 2 to be long, string given in /home/speedycm/public_html/speedyautos/cars_class.php on line 228*
警告:date() 期望参数 2 很长,在第 228 行的 /home/speedycm/public_html/speedyautos/cars_class.php 中给出的字符串*
cars_class.php reads this on line 228
car_class.php 在第 228 行读取此内容
$this->expiry_date = date("m/d/Y", $rows['expiry_date']);
how can i resolve this?
我该如何解决这个问题?
回答by Jacob
date()expects a unix timestamp... I imagine you are passing it a date as a string.
date()需要一个 unix 时间戳......我想你将一个日期作为字符串传递给它。
e.g. 2010-10-10
例如 2010-10-10
You should use:
你应该使用:
$this->expiry_date = date("m/d/Y", strtotime($rows['expiry_date']));
Or better yet, use the DateTimeobject.
或者更好的是,使用DateTime对象。
$expiry_date = new DateTime($rows['expiry_date']);
$this->expiry_date = $expiry_date->format('m/d/Y');
回答by Min
most databases nowadays return a datestring like this "2011-03-11 20:00:00".
现在大多数数据库都返回这样的日期字符串“2011-03-11 20:00:00”。
Easiest way (not always cheapest in terms of cpu usage) is:
最简单的方法(在 CPU 使用方面并不总是最便宜)是:
$this->expiry_date = date("m/d/Y", strtotime($rows['expiry_date']));