php - 将时间从mysql插入日期表行

标签 php mysql time

我有这段代码可以显示 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/

相关文章:

php - php 中的登录表单

php - 如何对数组元素进行分组?

php - sql 活出另一个表中的项目

mysql - 使某个项目仅在网页上显示一段时间

与通配符*匹配的php字符串?

php - 我无法使用 JSON 发送 POST 变量

python - 如何在按属性分组时查询对象集合

php - 隐藏和显示自定义标记在我的谷歌地图中不起作用

javascript - Javascript 中 iso 格式的日期时间

python - 嵌套循环或 'in' ,哪个更快?