Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Date 学习Yii2:数据转换_Date_Yii - Fatal编程技术网

Date 学习Yii2:数据转换

Date 学习Yii2:数据转换,date,yii,Date,Yii,我正忙着学习Yii2,看这本指南: 在我的客户表中,我有一个字段:customer\u birth,类型为:Date 正在尝试此代码: public function getBirthdayText() { return date('d/m/Y', $this->customer_birthday); } 正在运行我的页面,出现以下错误: 行中遇到格式不正确的数值: return date('d/m/Y', $this->customer_birthday); 为什么会

我正忙着学习Yii2,看这本指南:

在我的客户表中,我有一个字段:customer\u birth,类型为:Date

正在尝试此代码:

public function getBirthdayText()
{
    return date('d/m/Y', $this->customer_birthday);
}
正在运行我的页面,出现以下错误: 行中遇到格式不正确的数值:

return date('d/m/Y', $this->customer_birthday);

为什么会出现此错误?

Yii 2附带了非常有用的格式化程序,您可以使用它格式化日期

链接:

您应该首先正确配置格式化程序:


然后根据需要设置日期格式:

此->客户生日的价值是多少?PHPs
date()
只接受作为整数的unix时间戳。使用
date('d/m/Y',strotime($this->customer_birth))
$dt=新日期时间($this->customer\u生日);返回$dt->格式('d/m/Y')Hi@inseersa。谢谢你的回复。我将代码更改为:在我的控制器中有:$data=
Customers::find()->where(['customer\u active'=>1])->orderBy('customer\u name')->all()
在我的视图中:
foreach($val数据){$bday=date('d/m/Y',$val->customer_birth);echo“birth:$bday

”;}
收到错误:遇到格式不正确的数值,尝试了以下操作:
$bday=new Datetime($cust->customer_birth)出现此错误:无法将DateTime类的对象转换为string@chris,如我所说,该字段的类型为Date。值为:0000-00-00我是否应该将字段类型更改为整数?不,您不需要更改类型。但是您正在将一个字符串(0000-00-00)传递给PHPs
date()
,它需要一个整数。因此,只要将它转换为与
date()
一起使用,就像
returndate('d/m/Y',strotime($this->customer_birth))。确保
customer\u birth
包含有效日期。谢谢@aslawin,但我想了解我阅读的文档的每个步骤,所以我不想使用Formatter或其他任何东西,只想了解文档中使用的内容。