嗨,我正在做一个日历表,用来显示一年中的月份和一周中的几天。
我可以为这个月(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>发布于 2016-11-10 07:59:38
正如上述注释中所述,您可以使用嵌套在彼此内部的两个循环,一个用于年份,一个用于月份。这样,您就可以根据不同的日期(但当前的日期)执行已经给出的代码。
看看这个方法。它基本上完成了您正在寻找的内容,但是您需要对布局进行一些微调:
<?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);
}
}发布于 2016-11-11 07:46:01
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
]);
}我只得到这个。谢谢你的帮助!

https://stackoverflow.com/questions/40522182
复制相似问题