douxiduan8344 2013-11-28 15:54
浏览 93
已采纳

将PHP日期范围转换为MYSQL各个日期

I have an availability calendar in which I am currently adding in dates one by one, and using a mysql query to determine if there exists a row with a certain date and changing the class of the day to "booked" (Red).

I would like to enter in a range into my form, and process it through php (or mysql) into multiple, individual dates. My date format is M/D/YYYY, or MM/DD/YYYY, both are accepted. Unfortunately, when I built my calendar, I did not use the date format in sql for entries, but used varchar.

Is there a way to enter into my form for example 1/1/2014-1/3/2014 and have php convert that to 1/1/2014, 1/2/2014, 1/3/2014, and then have a mysql INSERT query to insert multiple values at once?

if (empty($_POST) === false && empty($errors) === true) {
$adcp_data = array(
'date'      => $_POST['date'],
'customer'  => $_POST['customer'],
'notes'     => $_POST['notes'],
            );
insert_adcp($adcp_data);
header('Location: adcp.php?success');
exit();

the insert_adcp function looks like this:

function insert_adcp ($adcp_data) {
    array_walk($adcp_data, 'array_sanitize');
    $fields = '`' . implode('`, `', array_keys($adcp_data)) . '`';
    $data = '\'' . implode('\', \'', $adcp_data) . '\'';

    mysql_query("INSERT INTO `adcp` ($fields) VALUES ($data)");

}

My workaround and last resort will be to add multiple text inputs and just add multiple dates manually so I only have to submit once. But a range is so much faster!

As a last note, if I could have those multiple entries keep the "customer" and "notes" values for each date in the range that would be amazing. I am prepared to lose those fields though to make this work. Thanks

  • 写回答

3条回答 默认 最新

  • du0173 2013-11-28 16:14
    关注

    Something like:

    $day = new DateTime($_POST['range_start']);
    $end = new DateTime($_POST['range_end']);
    
    $all_dates = array();
    
    while ($day <= $end){
      $all_dates[] = $day;
      $day->add(new DateInterval('P1D'));
    }
    

    That will give you an array of DateTime objects each of which represents a day in your range. You can get each object back into a string by calling DateTime::format() and passing 'm/d/Y' as the format string.

    As for getting multiple entries into MySQL, the INSERT syntax allows INSERT INTO table (column) VALUES (row1), (row2), ... (rowN)

    (this is clearly not not tested or the final code you would use -- just written into this web form from memory ... you'll have to write it out properly with input sanitation and range checking and whatnot.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line