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> </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>
執行結果php
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:00html
<< | < | > | >> | |||
日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|---|---|---|---|---|---|
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 |