dou47278 2016-08-08 00:34
浏览 160
已采纳

从查询中的日期时间中选择日期 - Yii2

I'm having one event table. In which, due date of event is being stored in datetime format. But, due to some change of requirement, now we need to show only date (excluding time) from due date column.

Event (Table)

id | user_id | description |       due_date          | is_completed

1      8        My Event1    2016-08-09 19:16:00          0
2      8        My Event2    2016-08-09 19:53:00          0

I wanted to show all event in date wise. Like all event under 2016-08-09.

So, I tried with this query.

$upcoming_events = Events::find()->select(['due_date'])->distinct()
           ->where(['user_id' => Yii::$app->users->getId(),'is_completed'=> 0 ])
           ->andWhere(['>=','due_date',date("Y-m-d")])
          ->orderBy(['due_date'=>'ASC'])->limit(5)->all();

But, now 2 dates are being selected as 2016-08-09 19:16:00 & 2016-08-09 19:53:00. Since, date part is not being fetched from select statement. It is showing 2 times same date.

var_dump($upcoming_events);

[1] => app\Events Object
  (
    [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [due_date] => 2016-08-09 19:16:00
        )
  )

[2] => app\Events Object
(

  [_attributes:yii\db\BaseActiveRecord:private] => Array
  (
      [due_date] => 2016-08-09 19:53:00
  )
)

How can I retrieve only date from date time field to get only 1 date in Yii2 Query.

Any help/hint/suggestions is appreciable.

展开全部

  • 写回答

4条回答 默认 最新

  • dongzhi9906 2016-08-08 01:52
    关注

    You can also use only the date part

     upcoming_events = Events::find()->select('date(due_date) as due_date')->distinct()
           ->where(['user_id' => Yii::$app->users->getId(),'is_completed'=> 0 ])
           ->andWhere(['>=','due_date',date("Y-m-d")])
          ->orderBy(['due_date'=>'ASC'])->limit(5)->all();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 excel函数公式问题
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
  • ¥15 el-select光标位置问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部