选择查询中的CAKEPHP3格式日期

选择查询中的CAKEPHP3格式日期,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我在我的项目中使用了CakePHP 3,我发现需要为我报告中的date\u joined和date\u inactive字段设置日期格式。我可以使用带有日期函数的本机select查询为字段设置日期格式,但在CakePHP中,我不确定如何将日期格式集成到select查询中 $query = $this->CustomersView->find(); $query->select(['id','contact_person',''date_joine

我在我的项目中使用了CakePHP 3,我发现需要为我报告中的
date\u joined
date\u inactive
字段设置日期格式。我可以使用带有日期函数的本机select查询为字段设置日期格式,但在CakePHP中,我不确定如何将日期格式集成到select查询中

$query = $this->CustomersView->find();
                $query->select(['id','contact_person',''date_joined',
                    'date_inactive','comments','status']);
                $query->toArray();
更新

我还尝试了CakePHP online中的一个示例

但这给我带来了以下错误:

'Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%m-%d-%y)) AS `datejoined`, CustomersView.date_inactive AS `CustomersView__date_' at line 1'
CakePHP生成的SQL查询:

SELECT CustomersView.id AS `CustomersView__id`,
CustomersView.contact_person AS `CustomersView__contact_person`, 
(date_format(date_joined, %m-%d-%y)) AS `datejoined`, 
CustomersView.date_inactive AS `CustomersView__date_inactive`, 
CustomersView.comments AS `CustomersView__comments`, 
CustomersView.status AS `CustomersView__status` 
FROM customers_view CustomersView
非常感谢您的帮助。:)

谢谢


Ray

如果日期字段在您的模式中属于适当的类型(例如,
DATETIME
),Cake将返回可以使用普通PHP格式化的DATETIME对象-您不需要在select中执行此操作

例如:

$query = $this->CustomersView->find();
$query->select(['id','contact_person','date_joined',
  'date_inactive','comments','status']);
$array = $query->toArray();

foreach ($array as $row) {
    echo $row["date_joined"]->format("dMY");
}

例如,假设您的查询只返回一行,此处的
date\u joined
字段设置为
2015-12-21 23:55:00
。上面的代码只需打印出
21Dec2015

如果日期字段在您的模式中是适当的类型(例如
DATETIME
),Cake将返回可以使用普通PHP格式化的日期时间对象-您不需要在select中执行此操作

例如:

$query = $this->CustomersView->find();
$query->select(['id','contact_person','date_joined',
  'date_inactive','comments','status']);
$array = $query->toArray();

foreach ($array as $row) {
    echo $row["date_joined"]->format("dMY");
}

例如,假设您的查询只返回一行,此处的
date\u joined
字段设置为
2015-12-21 23:55:00
。上面的代码只需打印出
2015年12月21日

即可解决以下代码的问题:

$query = $this->CustomersView->find();
$date = $query->func()->date_format([
                   'date_joined' => 'literal',
                    "'%m-%d-%y'" => 'literal'
                ]);
$query->select(['id', 'contact_person', 'date_joined' => $date,
                'date_inactive', 'comments', 'status']);
$query->toArray();

修复了以下代码的问题:

$query = $this->CustomersView->find();
$date = $query->func()->date_format([
                   'date_joined' => 'literal',
                    "'%m-%d-%y'" => 'literal'
                ]);
$query->select(['id', 'contact_person', 'date_joined' => $date,
                'date_inactive', 'comments', 'status']);
$query->toArray();

您可以使用MySQL中的日期格式($field,%d-%m-%Y)

这里是一个例子:

$query = $this->CustomersView->find();
                $query->select(['id','contact_person',''DATE_FORMAT(date_joined, "%d-%m-%Y")',
                    'date_inactive','comments','status']);

您可以使用MySQL中的日期格式($field,%d-%m-%Y)

这里是一个例子:

$query = $this->CustomersView->find();
                $query->select(['id','contact_person',''DATE_FORMAT(date_joined, "%d-%m-%Y")',
                    'date_inactive','comments','status']);

对于cakephp>3.0版本,我们可以使用带有项数组和别名的select函数作为键

$result = $this->Model->find('all')
         ->where(['column_1 = ' => 'xyz'])  
         ->select(['id','column_1','column_2','formatted_date'=>'DATE_FORMAT(date, "%Y-%m")']);

这里,
formatted\u date
date\u FORMAT
MySQL函数的别名。

对于cakephp>3.0版本,我们可以使用带有项目数组和别名的select函数作为键

$result = $this->Model->find('all')
         ->where(['column_1 = ' => 'xyz'])  
         ->select(['id','column_1','column_2','formatted_date'=>'DATE_FORMAT(date, "%Y-%m")']);

在这里,
formatted\u date
是MySQL函数的别名。

@ganareth,这肯定能用,但我正在寻找更简单的方法来实现日期格式,而不是在foreach循环中实现。我真的很感谢你的帮助。我的问题增加了更多内容。竖起大拇指:)相信我,这是最简单的方法。您不需要以这种方式进行foreach,但实际上最干净的处理方法是保留ORM返回的对象,然后在打印时格式化日期。@ganareth,这肯定会奏效,但我正在寻找更简单的方式来进行日期格式化,而不是在foreach循环中进行格式化。我真的很感谢你的帮助。我的问题增加了更多内容。竖起大拇指:)相信我,这是最简单的方法。您不需要以这种方式进行foreach,但实际上最干净的处理方法是保留ORM返回的对象,然后在打印时格式化日期。我认为如果使用“literal”,则需要在日期格式周围加引号,例如,
“%m-%d-%y”'=>'literal'
-如果查看原始SQL,可以看到日期格式字符串周围没有引号。谢谢。我昨天也注意到了。:)我认为,如果使用“literal”,则需要在日期格式周围加引号,例如,
“%m-%d-%y”=>“literal”
-如果查看原始SQL,可以看到日期格式字符串周围没有引号。谢谢。我昨天也注意到了。:)不工作。导致相同的Cake\I18n\FrozenDate对象不工作。生成相同的Cake\I18n\FrozenDate对象