【问题标题】:Omitting redundant months and years in date range list在日期范围列表中省略多余的月份和年份
【发布时间】:2013-04-18 06:31:41
【问题描述】:

我在活动日期方面遇到了奇怪的问题,我努力修复它但无法解决。

我附上了我想如何在页面上显示日期的屏幕截图:

在图片中的第一个事件 Deine Energie in Aktion!是 5 个事件的组合,每个事件都有其开始日期和结束日期。

活动的第一部分是为期 1 天的活动,从 4 月 4 日开始,到 4 月 4 日结束。同样,第二部分是 4 月 7 日,第三部分是 4 月 9 日,第四部分是 4 月 20 日

最后一部分从 5 月 5 日开始,到 5 月 10 日结束。

日期以这种格式存储在数据库中: 我正在显示活动最后一部分的日期。 活动开始日期:2013-05-05 00:00:00 活动结束日期:2013-05-10 00:00:00

所以我想以图片中显示的格式显示日期。

有多种情况: 首先,如果所有日期都在一个月内,那么我们只在末尾显示一次月份名称。 其次,如果月份更改,则月份名称将显示在更改月份的日期之后。

我在 while 循环中获取事件日期,那么如何在循环中将当前事件日期与即将到来的事件日期进行比较。

这是迄今为止我用来从数据库中获取日期的代码..

    $nid = $row->nid;  
$get_product_id = "SELECT product_id from {uc_product_kits} where nid='$nid'";
$res = db_query($get_product_id);
while ($get_product_id_array_value = db_fetch_array($res)) {
    $prductid = $get_product_id_array_value['product_id'];
    $start_date = db_query("select event_start,event_end from {event} where nid=%d",$prductid);
    $start_date_value = db_fetch_object($start_date);
    $end_value = $start_date_value->event_start;

    $event_end_date = $start_date_value->event_end;
    $TotalStart =  date("d M Y", strtotime($end_value));
    $TotalEnd = date("d M Y", strtotime($event_end_date));
    $onlyMonthStart =  date("M", strtotime($end_value));
    $onlyMonthEnd = date("M", strtotime($event_end_date));
    //$groupMonth = db_query("select event_start,event_end, month  from {event} where nid=%d group by ",$prductid);
    if($TotalStart == $TotalEnd ){
        $startDay = date("d", strtotime($end_value));
        $startMonth = date("M", strtotime($end_value));
        if(in_array($startMonth,$newMonth)) {
            echo $onlstartdate;
        } 
        else {
              $onlstartdate =  date("d", strtotime($end_value));
             echo $onlstartdate;
             $tempStorage[] = $startMonth
        }
        //$newMonth[] = $startMonth;
    }
}

【问题讨论】:

    标签: php date datetime formatting


    【解决方案1】:

    最简单的方法是首先将查询中的所有数据收集到例如数组。

    只有这样才能遍历数组。将所有数据放在一起可以让您比较两个连续的日期范围,以确定您需要为每个日期范围打印的详细信息。

    注释示例:

    // collect data from SQL query into structure like this:
    
    $events = array(
        array("event_start" => "2013-4-4", "event_end" => "2013-4-4"),
        array("event_start" => "2013-4-7", "event_end" => "2013-4-7"),
        array("event_start" => "2013-4-9", "event_end" => "2013-4-9"),
        array("event_start" => "2013-4-20", "event_end" => "2013-4-20"),
        array("event_start" => "2013-5-5", "event_end" => "2013-5-10"),
        array("event_start" => "2014-1-1", "event_end" => "2014-1-2"),
    );
    
    // the actual code for range list generation:
    
    for ($i = 0; $i < count($events); $i++)
    {
        // parse start and end of this range
        $this_event = $events[$i];
        $this_start_date = strtotime($this_event["event_start"]);
        $this_end_date = strtotime($this_event["event_end"]);
    
        // extract months and years
        $this_start_month = date("M", $this_start_date);
        $this_end_month = date("M", $this_end_date);
        $this_start_year = date("Y", $this_start_date);
        $this_end_year = date("Y", $this_end_date);
    
        $last = ($i == count($events) - 1);
        // parse start and end of next range, if any
        if (!$last)
        {
            $next_event = $events[$i + 1];
            $next_start_date = strtotime($next_event["event_start"]);
            $next_end_date = strtotime($next_event["event_end"]);
    
            $next_start_month = date("M", $next_start_date);
            $next_end_month = date("M", $next_end_date);
            $next_start_year = date("Y", $next_start_date);
            $next_end_year = date("Y", $next_end_date);
        }
    
        // ranges with different starting and ending months always go
        // on their own line
        if (($this_start_month != $this_end_month) ||
            ($this_start_year != $this_end_year))
        {
            echo date("j M", $this_start_date);
            // print starting year only if it differs from ending year
            if ($this_start_year != $this_end_year)
            {
                echo " ".date("Y", $this_start_date);
            }
            echo "-".date("j M Y", $this_end_year)." <br/>\n";
        }
        else
        {
            // this is range starting and ending in the same month
    
            echo date("j", $this_start_date);
            // different starting and ending day
            if ($this_start_date != $this_end_date)
            {
                echo "-".date("j", $this_end_date);
            }
    
            $newline = false;
            // print month for the last range;
            // and for any range that starts(=ends) in different month
            // than the next range ends
            if ($last ||
                ($this_start_month != $next_end_month))
            {
                echo " ".date("M", $this_start_date);
                $newline = true;
            }
    
            // print year for the last range;
            // and for any range that starts(=ends) in different year
            // than next range ends
            if ($last ||
                ($this_start_year != $next_end_year) ||
                ($next_start_month != $next_end_month))
            {
                echo " ".date("Y", $this_start_date);
                $newline = true;
            }
    
            if ($newline)
            {
                echo " <br/>\n";
            }
            else
            {
                // month (and year) will be printed for some future range
                // on the same line
                echo ", ";
            }
        }
    }
    

    这个输出:

    4, 7, 9, 20 Apr <br/>
    5-10 May 2013 <br/>
    1-2 Jan 2014 <br/>
    

    【讨论】:

      【解决方案2】:

      检查您是否需要为当前日期项目打印月份的一种可能性实际上是检查 下一个 项目。让我试着用伪代码解释一下:

      <?php
      
          $month = 0;  // Initialize $month variable to unset
      
          // Loop over all your events
          foreach($dates as $date) {
      
              // Convert $date to a timestamp
      
              // If the 'month' of the current $timestamp is unequal to $month
              // it means we switch months and we have to print the $month first
              if(date('m', $timestamp) != $month) {
      
                  echo $month;  // Of course format how you want it to be displayed
      
                  // Set $month to the new month
                  $month = date('m', $timestamp);
              }
      
              // Print the rest of the event, like day numbers here
      
          }
      ?>
      

      【讨论】:

        【解决方案3】:

        好吧,由于您需要比较一个循环与另一个循环的值,您将无法直接使用echo

        您需要使用临时变量。因此,在开始日期的第一个循环中,您存储$tmp_day_1$tmp_month_1,然后在结束日期循环中,您可以比较两个月份并检查它们是否不同。然后你可以使用echo。我希望我能表达我的观点:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-23
          • 2018-02-03
          • 1970-01-01
          • 2012-12-14
          • 2013-11-01
          • 1970-01-01
          相关资源
          最近更新 更多