php - 如何使用 codeigniter 将月份日期设置为 coloum 表
问题描述
我尝试在一个月内创建一个带有日期列的表,我想尝试将我拥有的日期输入到表中,但我不明白它的逻辑..
<?php
$Month = date('m');
$Year = date('Y');
$day = cal_days_in_month(CAL_GREGORIAN, $Month ,$Year);
$q = $this->db->select('nm_py, dt_start,dt_finish')->from('tb_payment')->get();
$data = $q->result_array();
//example data nm_py : Mobile, dt_start: 2018-05-02, dt_finish: 2018-05-05
//example table output as expected
// | NO| Payment | Month
// |___|_________| 1 | 2 | 3 | 4 | 5 | 6 | 7 | xxxxx
// | 1 | Mobile | - | Y | Y | Y | Y | - | - | xxxxxx
?>
<table>
<thead>
<tr>
<th class="text-center">No</th>
<th class="text-center">Payment</th>
<th class="text-center" colspan="<?php echo $day; ?>">Month</th>
</tr>
<tr>
<th class="text-left"></th>
<th class="text-left"></th>
<?php
for ($x = 1; $x <= $day; $x++) {
echo "<th class='text-center'>".$x."</th>";
}
?>
</tr>
<thead>
<tbody>
<?php $no = 1; foreach($data as $row) { ?>
<?php $no++; }?>
</tbody>
</table>
解决方案
您可以使用date_create
以下方式查看工作代码:
<?php
$q = $this->db->select('nm_py, dt_start,dt_finish')->from('tb_payment')->get();
$data = $q->result_array();
//example data nm_py : Mobile, dt_start: 2018-05-02, dt_finish: 2018-05-05
//example table output as expected
// | NO| Payment | Month
// |___|_________| 1 | 2 | 3 | 4 | 5 | 6 | 7 | xxxxx
// | 1 | Mobile | - | Y | Y | Y | Y | - | - | xxxxxx
?>
<table>
<thead>
<tr>
<th class="text-center">No</th>
<th class="text-center">Payment</th>
<th class="text-center" colspan="<?php echo $day; ?>">Month</th>
</tr>
<tr>
<th class="text-left"></th>
<th class="text-left"></th>
<?php
for ($x = 1; $x <= $day; $x++) {
echo "<th class='text-center'>".$x."</th>";
}
?>
</tr>
<thead>
<tbody>
<?php $no=1; foreach($data as $row) {
echo "<tr>";
echo "<td class='text-center'>".$no."</td>";
echo "<td class='text-center'>".$row['nm_py']."</td>";
// days values
$day_start=date_create($row['dt_start']);
$day_end=date_create($row['dt_finish']);
for ($x = 1; $x <= $day; $x++) {
if($x >=$day_start->format('d') and $x <=$day_end->format('d'))
echo "<td class='text-center'>Y</td>";
else
echo "<td class='text-center'>-</td>";
}echo "</tr>";
$no++;
}?>
</tbody>
</table>
推荐阅读
- c# - 使用反向 For 循环时如何判断我是否到达列表的开头?
- laravel - InvalidArgumentException:未找到查看 [主题。]
- javascript - 如何通过字节数组创建文件对象
- ios - AVFoundation- .audioDeviceInUseByAnotherClient 在调用 CXCallObserver call.hasEnded(呼叫断开)后运行
- google-apps-script - 为什么 indexOf() 不能使用谷歌表格在 .map() 内部工作?
- javascript - Angular 升级到 8 后出现拖动事件的问题。(使用 D3)
- r - 如何将字符日期(1931 年 2 月 12 日)转换为日期格式(Y、m、d)
- django - 有没有办法在 django 的另一个页面上保存数据?
- python-3.x - PYODBC - 类型错误:要执行的第一个参数必须是字符串或 unicode 查询
- python - 使用 JupyterNotebook 的问题