渲染日期和价格表
假设您以数组的形式给出了到达日期 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 行,因此我可以依靠循环遍历所有日子,但它变得更加复杂,因为我现在每周需要两行。
Let's say you were given the arrival date 12/28/10 and a departure date of 1/5/11, in the form of an array ( which is always consistent and can be relied upon ) such as:
$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
)
);
How could I generate a table summarising the two weeks of rates up, with the first table cell starting on Sunday and the last table cell on Saturday?
The output would need to be something like:
<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>
What I have so far done, is:
$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++) {
}
However I'm not sure if what I'm doing is right, that is looping through the weeks.. because I can't seem to get a reference to the day I want inside of that loop.
Previously, I did not need to generate the mm/dd row so I could rely on looping through all the days, but it's gotten more complex since I need two rows per week now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个,它有效...
try this, it works...