簡體   English   中英

將時間從mysql插入到表日期行

[英]Inserting time from mysql to table row of dates

我有這個代碼,將顯示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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM