php中博客日历实现代码

分享一款php中博客日历实现代码,里面有一句是查询数据库的这个很简单,只要你修改一下字段名与你数据库相同就行了,实例代码如下:

  1. function calendarnew($starttime = 0) {
  2. global $db, $tablepre, $timestamp, $site_timeoffset, $site_dateformat, $curtime;
  3. $starttime = $starttime ? $starttime : $timestamp;
  4. $curtime = gmdate($site_dateformat, $starttime + $timeoffset * 3600);
  5. $pendtime = $starttime - (gmdate('j', $starttime + $timeoffset * 3600) - 1) * 86400 - ($starttime + $timeoffset * 3600) % 86400;
  6. $pstarttime = $pendtime - gmdate('t', $pendtime + $timeoffset * 3600 - 1) * 86400;
  7. $nstarttime = $pendtime + gmdate('t', $pendtime + $timeoffset * 3600 + 1) * 86400;
  8. $nendtime = $nstarttime + gmdate('t', $nstarttime + $timeoffset * 3600 + 1) * 86400;
  9. list($skip, $dim) = explode('-', gmdate('w-t', $pendtime + $timeoffset * 3600 + 1));
  10. $rows = ceil(($skip + $dim) / 7);
  11. $blogs = array();
  12. $query = $db->query("SELECT dateline FROM {$tablepre}vitaitems WHERE dateline BETWEEN '$pendtime' AND '$nstarttime'");
  13. while($blog = $db->fetch_array($query)) {
  14. $day = gmdate('j', $blog['dateline'] + $timeoffset * 3600);
  15. !isset($blogs[$day]) ? $blogs[$day] = array('num' => 1, 'dateline' => $blog['dateline'] - $blog['dateline'] % 86400) : $blogs[$day]['num']++;
  16. }
  17. $cal = '';
  18. for($row = 0; $row < $rows; $row++) {
  19. $cal .= '<tr align="center" class="smalltxt">';
  20. for($col = 0; $col < 7; $col++) {
  21. $cur = $row * 7 + $col - $skip + 1;
  22. $curtd = $row * 7 + $col < $skip || $cur > $dim ? '&nbsp;' : $cur;
  23. if(!isset($blogs[$cur])) {
  24. $cal .= '<td>'.$curtd.'</td>';
  25. } else {
  26. $cal .= '<td><a href="blog.php?u&starttime='.$blogs[$cur]['dateline'].'&endtime='.($blogs[$cur]['dateline'] + 86400).'" title=" '.$blogs[$cur]['num'].' "><b>'.$cur.'</b></a></td>';
  27. }
  28. }
  29. $cal .= '';
  30. }
  31. return $cal;
  32. }