简单的PHP日历算法

By 深空, 2009年12月28日 15:22

  看看有没有比我更优的,HOHO。

/**
 * 日历
 *
 * Copyright(c) 2007 by 陈毅鑫(深空). All rights reserved
 * To contact the author write to {@link mailto:shenkong@php.net}
 * @author 陈毅鑫(深空)
 */
if (function_exists('date_default_timezone_set')) {
    date_default_timezone_set('Asia/Chongqing');
}
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$date = getdate(strtotime($date));
$end = getdate(mktime(0, 0, 0, $date['mon'] + 1, 1, $date['year']) - 1);
$start = getdate(mktime(0, 0, 0, $date['mon'], 1, $date['year']));
$pre = date('Y-m-d', $start[0] - 1);
$next = date('Y-m-d', $end[0] + 86400);
$html = '<table border="1">';
$html .= '<tr>';
$html .= '<td><a href="' . $PHP_SELF . '?date=' . $pre . '">-</a></td>';
$html .= '<td colspan="5">' . $date['year'] . ';' . $date['month'] . '</td>';
$html .= '<td><a href="' . $PHP_SELF . '?date=' . $next . '">+</a></td>';
$html .= '</tr>';
$arr_tpl = array(0 => '', 1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => '');
$date_arr = array();
$j = 0;
for ($i = 0; $i < $end['mday']; $i++) {
    if (!isset($date_arr[$j])) {
        $date_arr[$j] = $arr_tpl;
    }
    $date_arr[$j][($i+$start['wday'])%7] = $i+1;
    if ($date_arr[$j][6]) {
        $j++;
    }
}
foreach ($date_arr as $value) {
    $html .= '<tr>';
    foreach ($value as $v) {
        if ($v) {
            if ($v == $date['mday']) {
                $html .= '<td><b>' . $v . '</b></td>';
            } else {
                $html .= '<td>' . $v . '</td>';
            }
        } else {
            $html .= '<td>&nbsp;</td>';
        }
    }
    $html .= '</tr>';
}
$html .= '</table>';
echo $html;
VN:F [1.9.3_1094]
Rating: 7.8/10 (29 votes cast)
VN:F [1.9.3_1094]
Rating: +13 (from 25 votes)
简单的PHP日历算法, 7.8 out of 10 based on 29 ratings

10 Responses to “简单的PHP日历算法”

  1. 碧云轩 说:

    我想在我的网站http://www.zhan58.com/index.php远程file_get_contents这个页面http://bbs.zhan58.com/888.php,但是大部份时间得到的是http://bbs.zhan58.com/这个首页,我想知道是什么原因,是网速的原因吗,但是是静态的却可以

    如果以判断http://bbs.zhan58.com/888.php修改时间或者大小来自动生成静态页面,应该怎么写呢,谢谢

    VA:F [1.9.3_1094]
    Rating: 8.0/10 (2 votes cast)
    VA:F [1.9.3_1094]
    Rating: +1 (from 1 vote)
    • 文豆PHP 说:

      你的888.php 里可能有HTTP头信息的验证过滤,静态的就不会
      你可以尝试用其他方式去获取CURL,fscok…之类的

      VA:F [1.9.3_1094]
      Rating: 0.0/10 (0 votes cast)
      VA:F [1.9.3_1094]
      Rating: 0 (from 0 votes)
  2. 非常漂亮的代码!

    VA:F [1.9.3_1094]
    Rating: 4.0/10 (1 vote cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  3. 你的空间好慢啊,哪里的空间?上面的问题我已经解决了

    VA:F [1.9.3_1094]
    Rating: 1.0/10 (1 vote cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  4. 网赚 说:

    怎么这么久没有更新了?博主去哪里了?

    VA:F [1.9.3_1094]
    Rating: 0.0/10 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  5. xcl_code 说:

    相当精妙的思路。
    看完,获益匪浅。我决定先看完你的博文。

    VA:F [1.9.3_1094]
    Rating: 0.0/10 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  6. tom 说:

    我觉得你的有点复杂。

    <a href="”><    
    <a href="”>>

    SunMonTunWenThuFriSat
    <?php
    $current=1;
    while($current<=$total_days)
    {
    echo '’;
    for($i=0;$i<7;$i++)
    {
    if(($current==1&&$i$total_days)
    {
    echo ‘ ’;
    continue;
    }
    echo “$current”;
    $current++;
    }
    echo ”;
    }
    echo ”;
    ?>

    VA:F [1.9.3_1094]
    Rating: 0.0/10 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  7. tom 说:

    sorry, 忘转义了。
    <?php
    $timestamp=(isset($_GET['t']))?$_GET['t']:time();
    list($month, $day, $year)=explode(‘/’, date(‘m/d/Y’, $timestamp));
    $first_day_in_month=date(‘w’,mktime(0, 0, 0, $month, 1, $year));
    $total_days=date(‘t’, $timestamp);
    ?>
    <table>
    <tr><th colspan=”7″><a href=”<?php echo htmlspecialchars($_SERVER['PHP_SELF']).’?t=’.strtotime(‘-1 month’, $timestamp);?>”><</a>   <?php echo date(‘F ‘, $timestamp).$year;?>  
    <a href=”<?php echo htmlspecialchars($_SERVER['PHP_SELF']).’?t=’.strtotime(‘+1 month’, $timestamp);?>”>></a></th>
    </tr>
    <tr><th>Sun</th><th>Mon</th><th>Tun</th><th>Wen</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
    <?php
    $current=1;
    while($current<=$total_days)
    {
    echo ‘<tr>’;
    for($i=0;$i<7;$i++)
    {
    if(($current==1&&$i<$first_day_in_month)||$current>$total_days)
    {
    echo ‘<td> </td>’;
    continue;
    }
    echo “<td>$current</td>”;
    $current++;
    }
    echo ‘</tr>’;
    }
    echo ‘</table>’;
    ?>

    VA:F [1.9.3_1094]
    Rating: 0.0/10 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  8. peja 说:

    粗看了你第一行代码,就知道你是个垃圾程序员
    $date = isset($_GET['date']) ? $_GET['date'] : date(‘Y-m-d’);
    如果$_GET['data'] = ” “怎么办?你检测了么?
    isset函数好好去看看

    VA:F [1.9.3_1094]
    Rating: 6.0/10 (3 votes cast)
    VA:F [1.9.3_1094]
    Rating: -1 (from 3 votes)
    • 深空 说:

      本文主要讨论算法,具体如何检测自己写去。

      VN:F [1.9.3_1094]
      Rating: 0.0/10 (0 votes cast)
      VN:F [1.9.3_1094]
      Rating: 0 (from 0 votes)

Leave a Reply

京ICP备05002071号 ©2003-2010 深空