dongyue8640 2016-10-03 15:12
浏览 32
已采纳

如何使用带有指定格式字符串的doctrine 2的CURRENT_DATE()函数?

I have no idea how to set specified date format to query with CASE WHEN. What i try to do is to create a select that will look like one bellow.

$qb -> select('
              CASE WHEN ps.paymentDate >= (new \DateTime(\'%Y-%m-01\')) THEN \'true\' ELSE \'false\' END as myVar
');

I looked for the solution in the internet but i haven't found it. Am i thinking about the whole problem right?

Previous code in mysql looked like this:

IF(ps.payment_date >= DATE_FORMAT(CURDATE(),\'%Y-%m-1\'),\'true\',\'false\') as myVar   
  • 写回答

1条回答 默认 最新

  • dongzhanyan3667 2016-10-03 15:28
    关注

    Like this,

    $d = new \DateTime();
    $sd = $d->format('Y-m-01');
    $qb -> select("CASE WHEN ps.paymentDate >= '$sd' THEN 'true' ELSE 'false' END as myVar");
    

    Or

    $q = "IF(ps.payment_date >= DATE_FORMAT(CURDATE(),'%Y-%m-01'),'true','false') as myVar";
    

    NOTE: If you use single quotes inside a Double quoted string literal, you can dispense with all those escape characters which just server to confuse the eye. Also $variables are automatically expanded inside a double quoted string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题