【问题标题】:Rendering a table of dates with prices呈现带有价格的日期表
【发布时间】:2010-03-06 01:40:06
【问题描述】:

假设您的到达日期为 12/28/10 和出发日期为 1/5/11,以数组的形式(始终一致且可以依赖),例如:

$data = array(
0 => array(
'Date' => '12/28/10',
'Price' => 100
),
1 => array(
'Date' => '12/29/10',
'Price' => 100
),
2 => array(
'Date' => '12/30/10',
'Price' => 100
),
3 => array(
'Date' => '12/31/10',
'Price' => 100
),
4 => array(
'Date' => '1/1/11',
'Price' => 100
),
5 => array(
'Date' => '1/2/11',
'Price' => 100
),
6 => array(
'Date' => '1/3/11',
'Price' => 100
),
7 => array(
'Date' => '1/4/11',
'Price' => 100
)
);

如何生成一个表格来汇总两周的利率上涨,第一个表格单元格从星期日开始,最后一个表格单元格从星期六开始?

输出应该是这样的:

<table>
<tr>
        <thead>
                                                            <tr>
                                                            <th>Sun</th>
                                                            <th>Mon</th>
                                                            <th>Tue</th>

                                                            <th>Wed</th>
                                                            <th>Thu</th>
                                                            <th>Fri</th>
                                                            <th>Sat</th>
                                                            </tr>
                                                        </thead>
<tbody>
<tr>
<td>12/26</td>
<td>12/27</td>
<td>12/28</td>
<td>12/29</td>
<td>12/30</td>
<td>12/31</td>
<td>1/1</td>
</tr>
<tr>
<td>$100</td>
<td>$100</td>
<td>$100</td>
<td>$100</td>
<td>$100</td>
<td>$100</td>
<td>$100</td>
</tr>
<tr>
<td>1/2</td>
<td>1/3</td>
<td>1/4</td>
<td>1/5</td>
<td>1/6</td>
<td>1/7</td>
<td>1/8</td>
</tr>
<tr>
<td>$100</td>
<td>$100</td>
<td>$100</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

到目前为止,我所做的是:

$firstDay = strtotime( $data[0]['Date'] );
$firstSunday = strtotime('last sunday', $firstDay);
$lastDay = strtotime( $data[count($data)-1]);
$lastSaturday = strtotime('saturday', $lastDay);

$nights = (( $lastSaturday - $firstSunday ) / 86400) + 1;
$numWeeks = $nights / 7;

for ( $i=0; $i< $numWeeks; $i++) {
}

但是我不确定我所做的是否正确,那就是循环数周..因为我似乎无法在该循环中获得我想要的那一天的参考。

以前,我不需要生成 mm/dd 行,因此我可以依赖循环遍历所有天,但它变得更加复杂,因为我现在每周需要两行。

【问题讨论】:

    标签: php calendar date


    【解决方案1】:

    试试这个,效果很好……

            <?php
            $firstDay = strtotime( $data[0]['Date'] );
            $firstSunday = strtotime('last sunday', $firstDay);
            $diff = (( $firstDay - $firstSunday ) / 86400);
            $diff2 = 7 - (($diff+count($data))%7);
    
    
              for($i=0;$i<$diff;$i++){   
                //Adding missing elements in front of the array
                array_unshift($data, array('Date'=> date("m/d/Y",strtotime('-1 day',strtotime($data[0]['Date']))), 'Price'=>'0'));    
              }
              for($i=0;$i<$diff2;$i++){    
                //Adding missing elements in end of the array
                array_push($data, array('Date'=> date("m/d/Y",strtotime('+1 day',strtotime($data[count($data)-1]['Date']))), 'Price'=>'0'));    
              }
    
            //table header
            //cicle until the length of $data (num of days) array will reach
            //$i increment by 7 every cicle to simulate the week 
            //by this you don't need successive counts to retrive corret Date and price from array 
            for($i=0;$i<count($data);$i=$i+7){
                //Every week the $days and price are reset to null
                $days="";
                $price="";
                //cicle the 7 days in a week
                for($j=0;$j<7;$j++){
                    //add the columns Date ($i+$j) is used to retrive the correct array pos 
                    //$j is the day in the "current" week
                    //$i is the "past week" days 
                    $days.="<td>".$data[$i+$j]['Date']."</td>";
                    $price.="<td>".$data[$i+$j]['Price']."</td>";
                  }
                  //week finished add 2 rows and the respective cols
               echo "<tr>".$days."</tr><tr>".$price."</tr>";
            }
            //table Footer
    ?>
    

    【讨论】:

    • 哇,似乎工作得很好。你能解释一下内部的for循环吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多