简体   繁体   English

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

[英]Converting PHP date range to MYSQL individual dates

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). 我有一个可用性日历,当前正在其中逐个添加日期,并使用mysql查询来确定是否存在具有特定日期的行,并将当天的类别更改为“已预订”(红色)。

I would like to enter in a range into my form, and process it through php (or mysql) into multiple, individual dates. 我想在我的表单中输入一个范围,并通过php(或mysql)将其处理成多个单独的日期。 My date format is M/D/YYYY, or MM/DD/YYYY, both are accepted. 我的日期格式为M / D / YYYY或MM / DD / YYYY,两者都可以接受。 Unfortunately, when I built my calendar, I did not use the date format in sql for entries, but used varchar. 不幸的是,当我建立日历时,我没有使用sql中的日期格式输入条目,而是使用了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? 有没有一种方法可以进入我的表单,例如1/1 / 2014-1 / 3/2014并让php将其转换为1/1 / 2014、1 / 2 / 2014、1 / 3/2014,然后有一个mysql INSERT查询一次插入多个值?

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: insert_adcp函数如下所示:

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 谢谢

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. 这将为您提供一系列DateTime对象,每个对象代表您范围内的一天。 You can get each object back into a string by calling DateTime::format() and passing 'm/d/Y' as the format string. 您可以通过调用DateTime :: format()并将格式为'm / d / Y'的字符串传递回字符串中。

As for getting multiple entries into MySQL, the INSERT syntax allows INSERT INTO table (column) VALUES (row1), (row2), ... (rowN) 至于将多个条目输入MySQL,INSERT语法允许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.) (这显然没有经过测试,也没有最终使用的代码-只是从内存中写入此Web表单中……您必须使用输入环境卫生和范围检查等功能将其正确写出。)

Check if the value from the input match your range format, capture the parts and generate the from and to dates. 检查从输入值相匹配的范围格式,捕捉部分,并 日期产生。

if (preg_match('%\A(?<fromMonth>\d{1,2})/(?<fromDay>\d{1,2})/(?<fromYear>\d{4})-(?<toMonth>\d{1,2})/(?<toDay>\d{1,2})/(?<toYear>\d{4})\Z%', $str, $res)) {
    $dates['from'] = mktime(0, 0, 0, $res['fromMonth'], $res['fromDay'], $res['fromYear']);
    $dates['to']   = mktime(0, 0, 0, $res['toMonth'], $res['toDay'], $res['toYear']);
}

Generate the range between from and to dates. 生成介于和之间的日期。

for ($date = $dates['from']; $date <= $dates['to']; $date = strtotime('+1 day', $date) ){
    $dates['range'][] = date('m-d-Y', $date);
}

I think, strtotime is more usable for your case. 我认为, strtotime更适合您的情况。 You can found definition at php.net site: http://php.net/manual/en/function.strtotime.php 您可以在php.net网站上找到定义: http : //php.net/manual/en/function.strtotime.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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