简体   繁体   中英

Inserting time from mysql to table row of dates

i have this code that will display all the dates in the month of march of 2017

<?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>";

?>

if i have dates field in mysql with this following format

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

how do i insert the time above at each of it respective date in the table above

Try this:

  ///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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM