str_to_date函数在复杂的mysql查询joomla中不起作用

时间:2018-12-18 11:12:51

标签: php mysql joomla alias

我有一个sql查询女巫返回了3种最新形式,但是在我将fin_date列类型从字符串更改为具有特定格式的日期后,它不起作用

   $today = JFactory::getDate()->toFormat('%d/%m/%Y');
   // echo $today==> 18/12/2018

$query =  "SELECT p.fin_date, pfr.product_name, p.ville"
             ." FROM #__virtuemart_products as p"
             ." LEFT JOIN #__virtuemart_products_fr_fr as pfr on pfr.virtuemart_product_id = p.virtuemart_product_id"
             ." WHERE p.published = 1   AND  p.product_parent_id != 0 AND str_to_date(p.fin_date,'%d/%m/%Y') >= $today "
             ." LIMIT 4 "
             ;

即使我将其更改为:

$query =  "SELECT str_to_date(p.fin_date,'%d/%m/%Y'), pfr.product_name, p.ville"
             ." FROM #__virtuemart_products as p"
             ." LEFT JOIN #__virtuemart_products_fr_fr as pfr on pfr.virtuemart_product_id = p.virtuemart_product_id"
             ." WHERE p.published = 1   AND  p.product_parent_id != 0 AND str_to_date(p.fin_date,'%d/%m/%Y') >= $today "
             ." LIMIT 4 "
             ;

1 个答案:

答案 0 :(得分:1)

您无法将内部将以str_to_date格式的日期(Y-m-d的输出)与d/m/Y格式的字符串进行比较。您需要使用{p> 1以$today格式创建Y-m-d变量

$today = JFactory::getDate()->toFormat('%Y-%m-%d');

或在查询中将其转换,即

str_to_date(p.fin_date,'%d/%m/%Y') >= str_to_date($today,'%d/%m/%Y')