PHP学习笔记12——日期处理

  1 <?php
  2     //1. 关于时间的函数
  3         /* UNIX用一个整数表示时间,精确到秒
  4          * time()                                        获取当前UNIX时间戳,精确到秒
  5          * microtime([bool float])                        获取当前UNIX时间戳,精确到微妙(默认返回msec sec字符串,true时返回浮点数)
  6          * mktime(hour,mintue,second,month,day,year)    将时间转换成UNIX时间戳,如果无参数转换当前时间(相当于time).
  7          *                                                 也可以从右向左省略,省略的部分默认为当前时间
  8          * strtotime($time,[$now])                        相对now参数将自然语言时间转换成UNIX时间戳
  9          * getdate()                                    返回一个由各种时间内容组成的关联数组
 10          * 
 11          * date()                                        格式化输出时间,常用格式化字符如下
 12          * (Ymd His 年月日 时分秒,都有前导0) (w:周几,t:该月天数)
 13          * date_default_timezone_set('PRC')                设置时区为中国,也可以在php.ini中配置
 14          */    
 15     
 16     date_default_timezone_set("UTC");        //设置为格林威治标准时间
 17     
 18     echo microtime()."<br/>";
 19     echo microtime(true)."<br/>";
 20     echo time()."<br/>";
 21     echo mktime(0,0,0,1,1,1970)."<br/>";
 22     echo "<br/>";
 23     
 24     print_r(getdate()); echo "<br/>";
 25     print_r(getdate(mktime(10,31,50))); 
 26     echo "<br/><br/>";
 27     
 28     echo date("Y-m-d H:i:s", time())."<br/>";
 29     echo date("Y-m-d H:i:s", strtotime("May 8 2012"));
 30     
 31     //2. 日历类的设计
 32     date_default_timezone_set("PRC");
 33     class Calendar {
 34         private $year;
 35         private $month;
 36         private $start_weekday;        //该月第一天周几
 37         private $days;                //该月一共有多少天
 38         //初始化一些日期属性
 39         function __construct(){
 40             $this->year = isset($_GET['year']) ? $_GET['year'] : date('Y');
 41             $this->month = isset($_GET['month']) ? $_GET['month'] : date('m');
 42             $this->start_weekday = date('w', mktime(0,0,0,$this->month,1,$this->year));
 43             $this->days = date('t', mktime(0,0,0,$this->month,1,$this->year));
 44         }
 45         //打印日历
 46         function __toString(){
 47             $out = '<table align="center">';
 48             $out .= $this->changeDate();
 49             $out .= $this->weekList();
 50             $out .= $this->dayList();
 51             $out .= '</table>';
 52             return $out;
 53         }
 54         //用于用户操作调整年份和月份的设置
 55         private function changeDate($url="12.php"){
 56             $out = '<tr>';
 57             $out .= '<td><a href="'.$url.'?'.$this->prevYear($this->year, $this->month).'"><<</a></td>';
 58             $out .= '<td><a href="'.$url.'?'.$this->prevMonth($this->year, $this->month).'"><</a></td>';
 59             $out .= '<td colspan="3">';
 60             $out .= '<form>';
 61             $out .= '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
 62             for ($sy=1970; $sy <= 2038; $sy++){
 63                 $selected = ($sy==$this->year) ? "selected" : "";
 64                 $out .= '<option '.$selected.' value="'.$sy.'">'.$sy.'</option>';
 65             }
 66             $out .= '</select>';
 67             $out .= '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
 68             for ($sy=1; $sy <= 12; $sy++){
 69                 $selected = ($sy==$this->month) ? "selected" : "";
 70                 $out .= '<option '.$selected.' value="'.$sy.'">'.$sy.'</option>';
 71             }
 72             $out .= '</select>';
 73             $out .= '</form>';
 74             $out .= '</td>';
 75             $out .= '<td><a href="'.$url.'?'.$this->nextMonth($this->year, $this->month).'">></a></td>';
 76             $out .= '<td><a href="'.$url.'?'.$this->nextYear($this->year, $this->month).'">>></a></td>';
 77             $out .= '</tr>';
 78             return $out;
 79         }
 80         private function weekList() {
 81             $week = array('日','一','二','三','四','五','六');
 82             $out = '<tr>';
 83             foreach ($week as $x) {
 84                 $out .= '<th class="fontb">'.$x.'</th>';
 85             }
 86             $out .= '</tr>';
 87             return $out;
 88         }
 89         private function dayList() {
 90             $line = floor(($this->days + $this->start_weekday + 6) / 7);
 91             $out = "";
 92             for ($i = 0; $i < $line; $i++) {
 93                 $out .= '<tr>';
 94                 for ($j = 1; $j <= 7; $j++) {
 95                     $day =     $i * 7 + $j - $this->start_weekday;
 96                     if ($day < 1 || $day > $this->days)
 97                         $out .= '<td>&nbsp;</td>';
 98                     elseif ($day == date('d'))
 99                         $out .= '<td class="fontb">'.$day.'</td>';
100                     else 
101                         $out .= '<td>'.$day.'</td>';
102                 }
103                 $out .= '</tr>';
104             }
105             return $out;
106         }
107         private function prevYear($year, $month){
108             $year --;
109             if ($year < 1970) $year = 1970;
110             return "year={$year}&month={$month}";
111         }
112         private function prevMonth($year, $month){
113             $month --;
114             if ($month < 1) {$year --; $month = 12;}
115             if ($year < 1970) $year = 1970;
116             return "year={$year}&month={$month}";
117         }
118         private function nextYear($year, $month){
119             $year ++;
120             if ($year > 2038) $year = 2038;
121             return "year={$year}&month={$month}";
122         }
123         private function nextMonth($year, $month){
124             $month ++;
125             if ($month > 12) {$year++; $month = 1;}
126             if ($year > 2038) $year = 2038;
127             return "year={$year}&month={$month}";
128         }
129     }
130 ?>
131 
132 <html>
133     <head>
134         <style>
135             table {border: 1px solid #050;}
136             .fontb {color: white; background: blue;}
137             th {width: 30px;}
138             td,th {height: 30px; text-align: center;}
139             form {margin: 0px; padding: 0px;}
140         </style>
141     </head>
142     <body>
143         <?php echo new Calendar(); ?>
144     </body>
145 </html>

执行结果

 

0.03009700 1376630668
1376630668.0301
1376630668
0

Array ( [seconds] => 28 [minutes] => 24 [hours] => 5 [mday] => 16 [wday] => 5 [mon] => 8 [year] => 2013 [yday] => 227 [weekday] => Friday [month] => August [0] => 1376630668 )
Array ( [seconds] => 50 [minutes] => 31 [hours] => 10 [mday] => 16 [wday] => 5 [mon] => 8 [year] => 2013 [yday] => 227 [weekday] => Friday [month] => August [0] => 1376649110 )

2013-08-16 05:24:28
2012-05-08 00:00:00

<< <
> >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
posted @ 2013-08-16 13:26  Burn_E  阅读(260)  评论(0编辑  收藏  举报