本文介绍了如何在yii2中过滤日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想过滤3个月前,6个月前,1年之前的Date列。
我已经在gridview的搜索字段中创建了一个下拉菜单,如下所示。
I want to filter Date column like 3 months ago, 6 months ago, 1 year ago.I have created a dropdown in search field of gridview as given below.
[
'attribute' => 'modified',
'value' => 'name',
'filter' => array("ID1" => "Before Three months",
"ID2" => "Before six months",
"ID" => "Before Twelve months",),
],
,在modelsearch中,我想要搜索...
and in modelsearch I want to search like...
if (($this->modified) == "ID1"){
$query->andFilterWhere(['between', $this->modified, 'today', '3monthsago']);
}
但我不知道今天的3个月西米应该代替什么?
如何计算并在查询中传递此变量?
but I can't understand what should be there in place of today 3monthsago ?how to calculate and pass this variables in query??
推荐答案
将此添加到模型搜索中
$today = date('Y-m-d H:i:s',time());
if (($this->modified)=="ID1"){
$endDate = date('Y-m-d H:i:s', strtotime('-3 months'));
$query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]);
}elseif (($this->modified)=="ID2") {
$endDate = date('Y-m-d H:i:s', strtotime('-6 months'));
$query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]);
}elseif (($this->modified)=="ID") {
$endDate = date('Y-m-d H:i:s', strtotime('-12 months'));
$query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]);
}
这篇关于如何在yii2中过滤日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!