将PHP日期范围转换为MYSQL单个日期 [英] Converting PHP date range to MYSQL individual dates

查看:117
本文介绍了将PHP日期范围转换为MYSQL单个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可用性日历,当前我正在其中逐个添加日期,并使用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).

我想在表单中输入一个范围,然后通过php(或mysql)将其处理成多个单独的日期.我的日期格式为M/D/YYYY或MM/DD/YYYY,两者都可以接受.不幸的是,当我建立日历时,我没有使用sql中的日期格式输入条目,而是使用了varchar.

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.

是否有一种方法可以输入我的表单,例如1/1/2014-1/3/2014并让php将其转换为1/1/2014、1/2/2014、1/3/2014和然后有一个mysql INSERT查询一次插入多个值?

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();

insert_adcp函数如下所示:

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

推荐答案

类似的东西

$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'));
}

这将为您提供一系列DateTime对象,每个对象代表您范围内的一天.您可以通过调用DateTime :: format()并将"m/d/Y"作为格式字符串,来将每个对象重新转换为字符串.

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.

对于将多个条目输入MySQL,INSERT语法允许INSERT INTO table (column) VALUES (row1), (row2), ... (rowN)

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

(这显然没有经过测试,也不会使用最终代码-只是从内存中写入此Web表单...您必须通过输入卫生和范围检查等方式将其正确写出.)

(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.)

这篇关于将PHP日期范围转换为MYSQL单个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆