我有一个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 "
;
答案 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')