gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:32:03 27 4
gpt4 key购买 nike

基本上,我在我的本地 PC 上构建一个供个人使用的日历/事件预订系统,我的功能是从我的数据库中获取附有预订的日期,然后你可以相应地为数组中的日期重新着色, 但我正在尝试确定是否可以自定义每个范围内第一天的结果,并且接下来的几天是相同的。

这是它的样子(如果它使它更容易的话)

How it is now

Photoshop version of desired result

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

<?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>";

关于php - 分隔日期数组以使数组中的第一个日期具有不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29297871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com