Converting PHP date range to MYSQL individual dates

核能气质少年 提交于 2019-12-06 15:24:39

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

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. You can found definition at php.net site: http://php.net/manual/en/function.strtotime.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!