Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分隔日期数组以使数组中的第一个日期具有不同的输出_Php_Mysql_Arrays_Date - Fatal编程技术网

Php 分隔日期数组以使数组中的第一个日期具有不同的输出

Php 分隔日期数组以使数组中的第一个日期具有不同的输出,php,mysql,arrays,date,Php,Mysql,Arrays,Date,基本上,我在本地PC上构建了一个日历/活动预订系统供个人使用,我的功能是从数据库中获取附加了预订的天数,您可以相应地重新查询数组中的日期,但我正在尝试确定是否可以自定义每个范围中第一天的结果,接下来的日子也一样 下面是它的外观(如果它使它更容易) 这是我目前在页面上使用的代码 <?php function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) { $dates = array()

基本上,我在本地PC上构建了一个日历/活动预订系统供个人使用,我的功能是从数据库中获取附加了预订的天数,您可以相应地重新查询数组中的日期,但我正在尝试确定是否可以自定义每个范围中第一天的结果,接下来的日子也一样

下面是它的外观(如果它使它更容易)

这是我目前在页面上使用的代码

<?php 
function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {

$dates = array();
$current = strtotime($first);
$last = strtotime($last);

while( $current <= $last ) {

    $dates[] = date($output_format, $current);
    $current = strtotime($step, $current);
}
return $dates;
}
?>
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color: #aaaaaa;">
   <?php
  $cmonth = date('F'); 
  $cyear = date('Y');

$res = mysql_query("SELECT * FROM trips WHERE year(start_date) = '$cyear' ORDER BY start_date ASC");
$array_days = array();
while ($rows = mysql_fetch_array($res)) {

$selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));
foreach($selected_dates as $selected_date){
    $array_days[$selected_date]['start_end'] = "". $rows['trip_start'] . " to " . $rows['trip_end'];
    $array_days[$selected_date]['colour'] = "". $rows['colour'];
    $array_days[$selected_date]['booked'] = "". $rows['booked'];
    $array_days[$selected_date]['capacity'] = "". $rows['capacity'];
    }
}
?>
<tr>
     <td>&nbsp;</td>
     <?php for($i=1;$i<=31;$i++){?>
        <td class="trip-date-cell"><?php echo $i;?></td>
     <?php }?>
</tr>
<?php 
$current_month = date("m");
$next_6_month = date("m", strtotime("+5 month", strtotime(date("F") . "1")));
for($i=$current_month;$i<=$next_6_month;$i++){  // 12 months in year
?>
 <tr>
 <td class="trip-month-cell"><?php echo date('M', mktime(0, 0, 0, $i,10, $cyear)); ?></td>
<?php 
$days_in_month = cal_days_in_month(CAL_GREGORIAN,$i,$cyear);

foreach (range(1, $days_in_month) as $days) {
    $key = date('Y-m-d', mktime(0, 0, 0, $i, $days, $cyear));
    if(array_key_exists($key,$array_days)){
    $color = $array_days[$key]['capacity'] - $array_days[$key]['booked'] == 0 ? "#303030" : $array_days[$key]['colour'];
    echo "<td class='trip-day-book' style='background-color: ".$color."' alt='".$array_days[$key]['start_end']."' title='".$array_days[$key]['start_end']."'>&nbsp;</td>";
    } else {
    echo "<td class='trip-day-blank'>&nbsp;</td>";
  }
}
?>
</tr>
<?php } ?>
 </table>


读取日期范围时,可以将第一个日期信息存储在数组中:

while ($rows = mysql_fetch_array($res)) {

    $selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));

    // default to true: first date is first day in range
    $firstDate = true;

    foreach($selected_dates as $selected_date) {

        // store first day info in array
        $array_days[$selected_date]['first_day'] = $firstDate;

        // set to false for all following days
        if ($firstDate) $firstDate = false;

        $array_days[$selected_date]['start_end'] = "". $rows['trip_start'] . " to " . $rows['trip_end'];
        $array_days[$selected_date]['colour'] = "". $rows['colour'];
        $array_days[$selected_date]['booked'] = "". $rows['booked'];
        $array_days[$selected_date]['capacity'] = "". $rows['capacity'];
    }
}
然后在渲染时访问信息以获得所需的结果:

$color = $array_days[$key]['capacity'] - $array_days[$key]['booked'] == 0 ? "#303030" : $array_days[$key]['colour'];
// set content of cell according to first day info
$content = $array_days[$key]['first_day'] ? '<strong>1</strong>' : '&nbsp;'; 
echo "<td class='trip-day-book' style='background-color: ".$color."' alt='".$array_days[$key]['start_end']."' title='".$array_days[$key]['start_end']."'>$content</td>";
$color=$array\u days[$key]['capacity']-$array\u days[$key]['booked']==0?“#303030”:$array_days[$key]['color'];
//根据第一天信息设置单元格内容
$content=$array\u days[$key]['first\u day']?'1”:“”;
回显“$content”;