Home >Backend Development >PHP Tutorial >Blog calendar implementation code in php
Share a blog calendar implementation code in php. There is a sentence in it that querying the database is very simple. As long as you modify the field name to be the same as your database, the example code is as follows:
function calendarnew($starttime = 0) {
global $db, $tablepre, $timestamp, $site_timeoffset, $site_dateformat, $curtime;
$starttime = $starttime ? $starttime : $timestamp;
$curtime = gmdate($site_dateform at, $ starttime + $timeoffset * 3600);
$pendtime = $starttime - (gmdate('j', $starttime + $timeoffset * 3600) - 1) * 86400 - ($starttime + $timeoffset * 3600) % 86400 ;
$pstarttime = $pendtime - gmdate('t', $pendtime + $timeoffset * 3600 - 1) * 86400;
$nstarttime = $pendtime + gmdate('t', $pendtime + $ timeoffset * 3600 + 1) * 86400;
$nendtime = $nstarttime + gmdate('t', $nstarttime + $timeoffset * 3600 + 1) * 86400;
list($skip, $dim) = explode(' -', gmdate('w-t', $pendtime + $timeoffset * 3600 + 1));
$rows = ceil(($skip + $dim) / 7);
$blogs = array( );
$query = $db->query("SELECT dateline FROM {$tablepre}vitaitems WHERE dateline BETWEEN '$pendtime' AND '$nstarttime'");
while($blog = $ db->fetch_array($query)) {
$day = gmdate('j', $blog['dateline'] + $timeoffset * 3600);
!isset($blogs[$day ]) ? $blogs[$day] = array('num' => 1, 'dateline' => $blog['dateline'] - $blog['dateline'] % 86400) : $blogs[$day ]['num']++;
}
$cal = '';
for($row = 0; $row < $rows; $row++) {
$cal .= '
for($col = 0; $col < 7; $col++) {
$cur = $row * 7 + $col - $skip + 1 ;
$curtd = $row * 7 + $col < $skip || $cur > $dim ? ' ' : $cur;
if(!isset($blogs[$cur])) {
$cal .= '
} else {
$cal .= '
}
}
$cal .= '';
}
return $cal;
}