您好我正在制作一个日历表,显示一年中的月份和一周中的几天。
本月能够制作一个(2016年11月)。现在,我希望在今年和未来几年内完成它。
有人能帮助我吗?
<?php
/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");
/* Set the date */
$date = strtotime(date("Y-m-d"));
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
// $nextyear = strtotime('+1 month', $date);
$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = array();
for ($i = 0; $i < 32; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>
<table class='table table-bordered' style="table-layout: fixed;">
<tr>
<th colspan="32" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
</tr>
<tr>
<?php foreach($weekDays as $key => $weekDay) : ?>
<td class="text-center"><?php echo $weekDay ?></td>
<?php endforeach ?>
</tr>
<tr>
<?php for($i = 0; $i < $blank; $i++): ?>
<td></td>
<?php endfor; ?>
<?php for($i = 1; $i <= $daysInMonth; $i++): ?>
<?php if($day == $i): ?>
<td><strong><?php echo $i ?></strong></td>
<?php else: ?>
<td><?php echo $i ?></td>
<?php endif; ?>
<?php if(($i + $blank) % 32 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $blank + $daysInMonth) % 32 != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>
答案 0 :(得分:2)
正如上面的评论中所说,你可以使用彼此嵌套的两个循环,一个用于多年,一个用于几个月。这样,您可以根据不同的日期执行已经给定的代码,但是当前的代码。
看看这种方法。它基本上可以满足您的需求,但您需要对布局进行一些微调:
<?php
define('NUMBER_OF_COLUMNS', 37);
function renderCalenderMonth($date) {
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = array();
for ($i = 0; $i < NUMBER_OF_COLUMNS; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>
<table class='table table-bordered' style="table-layout: fixed;">
<tr>
<th colspan="<?php echo NUMBER_OF_COLUMNS?>" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
</tr>
<tr>
<?php foreach($weekDays as $key => $weekDay) : ?>
<td class="text-center"><?php echo $weekDay ?></td>
<?php endforeach ?>
</tr>
<tr>
<?php for($i = 0; $i < $blank; $i++): ?>
<td></td>
<?php endfor; ?>
<?php for($i = 1; $i <= $daysInMonth; $i++): ?>
<?php if($day == $i): ?>
<td><strong><?php echo $i ?></strong></td>
<?php else: ?>
<td><?php echo $i ?></td>
<?php endif; ?>
<?php if(($i + $blank) % NUMBER_OF_COLUMNS == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $blank + $daysInMonth) % NUMBER_OF_COLUMNS != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>
<?php
}
// ===========================
/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");
for ($iterateYear=2016; $iterateYear<2018; $iterateYear++) {
for ($iterateMonth=1; $iterateMonth<=12; $iterateMonth++) {
/* Set the date */
$date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
renderCalenderMonth($date);
}
}
答案 1 :(得分:0)
Laravel 5.3版日历代码:
public function index()
{
$users = DB::table('users')->where('roles_id', '2')->get();
date_default_timezone_set("Asia/Hong_Kong");
for ($iterateYear = 2017; $iterateYear < 2018; $iterateYear++) {
for ($iterateMonth = 1; $iterateMonth <= 12; $iterateMonth++) {
/* Set the date */
$date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
return $this->renderCalenderMonth($date);
}
}
return view('transaction.index', [
'users' => $users
]);
}
public function renderCalenderMonth($date)
{
// $date = strtotime(date("Y-m-d"));
$NUMBER_OF_COLUMNS = 37;
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = [];
for ($i = 0; $i < $NUMBER_OF_COLUMNS; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
$divid = $title . $year;
return view('transaction.index', [
'date' => $date,
'day' => $day,
'month' => $month,
'year' => $year,
'firstDay' => $firstDay,
'title' => $title,
'dayOfWeek' => $dayOfWeek,
'daysInMonth' => $daysInMonth,
'timestamp' => $timestamp,
'weekDays' => $weekDays,
'blank' => $blank,
'divid' => $divid,
'NUMBER_OF_COLUMNS' => $NUMBER_OF_COLUMNS
]);
}
我只是得到这个。谢谢你的帮助!