我有这段代码可以显示 2017 年 3 月的所有日期
<?php
$month = "03";
$year = "2017";
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime("+1 month", $start_time);
echo "<table border='1'>";
for($i=$start_time; $i<$end_time; $i+=86400)
{
$list = date('Y-m-d-D', $i);
echo "<tr><td>";
echo $list;
echo "</td><td>** time here ** </td></tr>";
}
echo "</table>";
?>
如果我在 mysql 中有以下格式的日期字段
3/1/2017 6:32:12 AM
3/12/2017 1:31:41 PM
3/13/2017 2:30:12 PM
3/17/2017 5:33:21 PM
3/19/2017 6:32:54 PM
3/16/2017 7:22:11 PM
3/21/2017 10:12:41 PM
我如何在上面表格中的每个相应日期插入上面的时间
最佳答案
试试这个:
///Your mysql data
$data = array(
'3/1/2017 6:32:12 AM',
'3/12/2017 1:31:41 PM',
'3/13/2017 2:30:12 PM',
'3/17/2017 5:33:21 PM',
'3/19/2017 6:32:54 PM',
'3/16/2017 7:22:11 PM',
'3/21/2017 10:12:41 PM'
);
$month = "03";
$year = "2017";
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime("+1 month", $start_time);
echo "<table border='1'>";
for($i=$start_time; $i<$end_time; $i+=86400)
{
$times = getTime($i, $data);
$list = date('Y-m-d-D', $i);
echo "<tr><td>";
echo $list;
echo "</td><td>".implode(",", $times)."</td></tr>";
}
echo "</table>";
function getTime($start_time, $data){
$time_arr = array();
foreach ($data as $d) {
$r = date('Y-m-d', strtotime($d));
if($r == date('Y-m-d', $start_time)){
$time_arr[] = date('H:i:s A', strtotime($d));
}
}
return $time_arr;
}
关于php - 将时间从mysql插入日期表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44004852/