按日期分组 - 日期时间问题

时间:2011-02-19 09:07:01

标签: php sql smarty

伙计们,我真的需要你的帮助。

我有一个使用smarty的文章列表,我想按日期组织,

Date1
ArticleA
Date2
ArticleB
ArticleC

我遇到的问题是在Date2上(或者如果同一天发布的文章超过1篇),整个Date2再次出现......即

Date1
ArticleA
Date2
ArticleB
ArticleC
Date2
ArticleB
ArticleC

我认为问题在于,Date2是发布文章的日期时间,因此每次重复的原因是因为时间值不同。

我需要找到一种方法来从datetime中删除时间值,或以某种方式操纵它来显示日期。

这是我的代码

SQL

$sql = 'select ' . TABLE_ADS . ('' . '.*' . $mlang_vars . ', ') . TABLE_ADS . '.id as adid, ' . TABLE_ADS_PICTURES . '.picture, ' . TABLE_ADS_PICTURES . '.`id` as `picture_id`, date_format(' . TABLE_ADS . ('' . '.`date_added`,\'' . $date_format . '\') as date_nice, date_format(') . TABLE_ADS . ('' . '.`date_expires`,\'' . $date_format . '\') as date_expires_nice, (') . TABLE_ADS . ('' . '.date_expires < \'' . $timestamp . '\' and ') . TABLE_ADS . '.date_expires!=\'0000-00-00 00:00:00\' and ' . TABLE_ADS . '.active=0) as expired, ' . TABLE_CATEGORIES . '_lang.name as category, ' . TABLE_PACKAGES . '.amount as amount, ' . TABLE_USERS . ('' . '.username as username ' . $pri_name . ' ' . $video . ' ' . $q_vars . ' from ') . TABLE_ADS . ' 
    LEFT JOIN ' . TABLE_CATEGORIES . '_lang on (' . TABLE_ADS . '.category_id=' . TABLE_CATEGORIES . '_lang.id and ' . TABLE_CATEGORIES . ('' . '_lang.`lang_id` = \'' . $crt_lang . '\')  
    LEFT JOIN ') . TABLE_PACKAGES . ' on ' . TABLE_ADS . '.package_id=' . TABLE_PACKAGES . '.id  
    LEFT JOIN ' . TABLE_USERS . ' on ' . TABLE_ADS . '.user_id=' . TABLE_USERS . '.id  
    LEFT JOIN ' . TABLE_ADS_PICTURES . ' on ' . TABLE_ADS . '.id=' . TABLE_ADS_PICTURES . ('' . '.ad_id 
    ' . $pri_join . ' 
    ' . $video_join . ' 
    ' . $q_join . ' 
    ') . $where . ' group by ' . TABLE_ADS . '.id ' . $order_by . ' ' . $order_way . ' '; 

PHP

{foreach from=$listings_array name="date_nice" item=k}{$k.date_nice} 
{foreach from=$listings_array  name="listing" item=v} 
   {if $k.date_nice==$v.date_nice} 
        {include file="short_listing.html"} 
   {/if} 
{/foreach} 
{/foreach} 

1 个答案:

答案 0 :(得分:1)

您可以将选择的第一行更改为

$sql = 'select ' . TABLE_ADS . ('' . '.*' . $mlang_vars . ', ') . TABLE_ADS . '.id as adid, ' . TABLE_ADS_PICTURES . '.picture, ' . TABLE_ADS_PICTURES . '.`id` as `picture_id`, date_format(' . TABLE_ADS . ('' . '.DATE(`date_added`),\'' . $date_format . '\') as date_nice, date_format(') . TABLE_ADS . ('' . '.DATE(`date_expires`),\'' . $date_format . '\') as date_expires_nice, (') . TABLE_ADS . ('' . '.date_expires < \'' . $timestamp . '\' and ') . TABLE_ADS . '.date_expires!=\'0000-00-00 00:00:00\' and ' . TABLE_ADS . '.active=0) as expired, ' . TABLE_CATEGORIES . '_lang.name as category, ' . TABLE_PACKAGES . '.amount as amount, ' . TABLE_USERS . ('' . '.username as username ' . $pri_name . ' ' . $video . ' ' . $q_vars . ' from ') . TABLE_ADS . ' 

基本上使用DATE()来处理日期部分