<?php //如果您提交了时间则显示您提交年月的日历,否则显示当前月份日历
if (isset($_GET['month']) && isset($_GET['year']))
{
$month = $_GET['month'];
$year = $_GET['year'];
}
else
{
$month = date ('m');
$year = date ('Y');
}
$weekid = date ('w',mktime(0,0,0,$month,1,$year));//某年某月第一天是星期几。0-7分别代表星期日-星期六
$countdays = date('t',mktime(0,0,0,$month,1,$year));//某年某个月的天数
//获取上个月的天数
$prevMonth = $month - 1;
$prevYear = $year;
if($prevMonth == 0){
$prevMonth = 12;
$prevYear = $year - 1;
}
$prevMonthDays = date('t',mktime(0,0,0,$prevMonth,1,$prevYear));//某年某个月的上个月的天数
//echo $prevMonthDays."<br/>";
//echo $prevMonth."<br/>";
$countdays = date('t',mktime(0,0,0,$month,1,$year));//某年某个月的天数
$arr_days = array ();//数组$arr_days代表某个月的每一天
//echo $weekid."<br/>";
//echo $countdays."<br/>";
//print_r($arr_days);
$tempweekid = $weekid-1; //用于计算上个月前面的天数
//初始化数组$arr_days
for ($i = 0; $i <= 35; $i++)
{
if($tempweekid >=0){
$arr_days[$i] = $prevMonthDays - $tempweekid;
$tempweekid--;
}else{
$arr_days[$i] = "";
} } //给$arr_days数组赋值
for ($i = $weekid, $j = 1; $j <= $countdays; $i++, $j++)
{
$arr_days[$i] = $j;
} header("Content-type: image/png");
$im = @imagecreate(300, 185)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255); // Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 37, 37);
$color2 = imagecolorallocatealpha($im,0,0,0,0); $alphagray = imagecolorallocatealpha($im,205,206,206,0);
$alphared = imagecolorallocatealpha($im,255,206,206,0); $font = './gillsans.ttf';
$text = "SU"; $titleArray = array("SUN","MON","TUE","WED","THU","FRI","SAT");
$font_x = 20;
$font_y = 20;
foreach($titleArray as $key=>$value){
// Add some shadow to the text
imagettftext($im, 12, 0, $font_x, $font_y, $color2, $font, $value);
$font_x +=40;
} $font_x = 20;
$font_y = 45; //表格输出
for ($i = 0; $i <= 35; $i++)
{
$row = $arr_days[$i];
$tempFontx = $font_x;
if($row < 10){
$tempFontx +=5;
} if ($i % 7 == 0)
{ if($i < 7 && $row > 7){
imagettftext($im, 12, 0, $tempFontx, $font_y, $alphared, $font, $row);
}else{
imagettftext($im, 12, 0, $tempFontx, $font_y, $red, $font, $row);
} $font_x +=40;
}else{
if($i < 7 && $row > 7){
imagettftext($im, 12, 0, $tempFontx, $font_y, $alphagray, $font, $row);
}else{
imagettftext($im, 12, 0, $tempFontx, $font_y, $color2, $font, $row);
} $font_x +=40;
} if (($i + 1) % 7 == 0)
{
$font_x = 20;
$font_y += 25;
}
} // Add the text
imagepng($im,"./".$year.$month.".png");
//imagepng($im);
imagedestroy($im);
使用的字体Font: http://pan.baidu.com/s/1jGBzrM2
最终效果:
参考文章:用php实现的一个简单万年历