標籤:計算 too 沒有 構造 等價 html 日曆 head universal
時間戳記:UNIX時間戳記是儲存日期和時間的一種緊湊簡潔的方法,是大多數UNIX系統中儲存當前日期和時間的一種方法,也是在大多數電腦語言中表示日期和時間的一種標準格式。UNIX時間戳記是從1970年1月1日0點(UTC/GMT的午夜)開始起到目前時間所經過的秒數
因為UNIX時間戳記是一個32位的數字格式,所以特別適用於電腦處理,例如計算兩個時間點之間相差的天數。另外,由於文化和地區的差異,存在不同的時間格式,以及時區的問題。所以,UNIX時間戳記也是根據一個時區進行標準化而設計的一種通用格式,並且這種格式可以很容易地轉換為任何格式
也因為UNIX時間戳記是一個32位的整數表示的,所以在處理1902年以前或2038年以後的事件,將會遇到一些問題。另外,在Window下,由於時間戳記不能為負數,如果使用PHP中提供的時間戳記函數處理1970年之前的日期,就會發生錯誤。要使PHP代碼具有可移植性,必須牢記這一點
【strtotime()】
strtotime()預期接受一個包含美國英語日期格式的字串並嘗試將其解析為Unix時間戳記(自January 1 1970 00:00:00 GMT 起的秒數),其值相對於now參數給出的時間,如果沒有提供此參數則用系統目前時間
int strtotime ( string $time [, int $now = time() ] )
<?phpecho strtotime("now"), "\n";//1488259922echo strtotime("10 September 2000"), "\n";// 968536800echo strtotime("+1 day"), "\n";//1488346322echo strtotime("+1 week"), "\n";//1488864722echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";//1489051924echo strtotime("next Thursday"), "\n";//1488409200echo strtotime("last Monday"), "\n";//1488150000?>
<?php $t ="1989-03-01 01:00:00"; echo strtotime($t);//604713600 date_default_timezone_set(‘PRC‘); $t ="1989-03-01 01:00:00"; echo strtotime($t);//604688400?>
【time()】
time()返回當前的 Unix 時間戳記
int time ( void )
<?php$nextWeek = time() + (7 * 24 * 60 * 60);// 7 days; 24 hours; 60 mins; 60 secsecho ‘Now: ‘. date(‘Y-m-d‘) ."\n";//Now: 2017-02-28echo ‘Next Week: ‘. date(‘Y-m-d‘, $nextWeek) ."\n";//Next Week: 2017-03-07echo ‘Next Week: ‘. date(‘Y-m-d‘, strtotime(‘+1 week‘)) ."\n";// Next Week: 2017-03-07?>
【date()】
date()格式化一個本地時間/日期,返回將整數timestamp按照給定的格式字串而產生的字串。如果沒有給出時間戳記則使用本地目前時間。換句話說,timestamp是可選的,預設值為 time()
string date ( string $format [, int $timestamp ] )
format參數如下所示
日d 月份中的第幾天,有前置字元為零的 2 位元字 01 到 31D 星期中的第幾天,文本表示,3 個字母 Mon 到 Sunj 月份中的第幾天,沒有前置字元為零 1 到 31l 星期幾,完整的文字格式設定 Sunday 到 SaturdayN ISO-8601 格式數字表示的星期中的第幾天 1(表示星期一)到 7(表示星期天)S 每月天數後面的英文尾碼,2 個字元 st,nd,rd 或者 th。可以和 j 一起用w 星期中的第幾天,數字表示 0(表示星期天)到 6(表示星期六)z 年份中的第幾天 0 到 365星期W ISO-8601 格式年份中的第幾周,每周從星期一開始 42(當年的第 42 周)月 F 月份,完整的文字格式設定, January或March January 到 Decemberm 數字表示的月份,有前置字元為零 01 到 12M 三個字母縮寫表示的月份 Jan 到 Decn 數字表示的月份,沒有前置字元為零 1 到 12t 給定月份所應有的天數 28 到 31年 L 是否為閏年 如果是閏年為 1,否則為 0o ISO-8601 格式年份數字 1999 or 2003Y 4 位元字完整表示的年份 1999 或 2003y 2 位元字表示的年份 99 或 03時間 a 小寫上午和下午值 am 或 pmA 大寫的上午和下午值 AM 或 PMB Swatch Internet 標準時 000 到 999g 小時,12 小時格式,沒有前置字元為零 1 到 12G 小時,24 小時格式,沒有前置字元為零 0 到 23h 小時,12 小時格式,有前置字元為零 01 到 12H 小時,24 小時格式,有前置字元為零 00 到 23i 有前置字元為零的分鐘數 00 到 59s 秒數,有前置字元為零 00 到 59u 毫秒 654321時區 e 時區標識(PHP 5.1.0 新加) UTC,GMT,Atlantic/AzoresI 是否為夏令時 如果是夏令時為 1,否則為 0O 與格林威治時間相差的小時數 +0200P 小時和分鐘之間有冒號分隔 +02:00T 本機所在的時區 EST,MDTZ 時差位移量的秒數 -43200 到 43200完整的日期/時間c ISO 8601 格式的日期 2004-02-12T15:19:21+00:00r RFC 822 格式的日期 Thu, 21 Dec 2000 16:01:07 +0200U 從 Unix 紀元(January 1 1970 00:00:00 GMT)開始至今的秒數
<?php// 設定要用的預設時區。自 PHP 5.1 可用date_default_timezone_set(‘UTC‘);//Tuesdayecho date("l");// Tuesday 28th of February 2017 05:41:19 AMecho date(‘l dS \of F Y h:i:s A‘);//July 1, 2000 is on a Saturdayecho "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));// Tue, 28 Feb 2017 05:41:19 +0000echo date(DATE_RFC2822);// 2000-07-01T00:00:00+00:00echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));?>
<?php $t = time(); echo date("Y-m-d H:i:s")."<br>";//2017-02-28 06:56:42 echo date("Y/m/d H:i:s", $t)."<br>";//2017/02/28 06:56:42 echo date("Y年m月d日 H:i:s", $t)."<br>";//2017年02月28日 06:56:42?>
【mktime()】
在PHP中,如果需要將日期和時間轉換為UNIX時間戳記,可以調用mktime()函數
mktime()Unix時間戳記,參數可以從右向左省略,任何省略的參數會被設定成本地日期和時間的當前值
int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
<?php// Prints something like: 2006-04-05T01:02:03+00:00echo date(‘c‘, mktime(1, 2, 3, 4, 5, 2006));?>
mktime()在做日期計算和驗證方面很有用,它會自動計算超出範圍的輸入的正確值
<?php $t = mktime(1, 0, 0, 01, 03, 1989); echo date(‘y-m-d H:i:s‘,$t) .‘<br>‘;//89-01-03 01:00:00 $t = mktime(1, 0, 0, 01, 43, 1989); echo date(‘y-m-d H:i:s‘,$t) .‘<br>‘;//89-02-12 01:00:00?>
時區
每個地區都有自己的本地時間,在網上及無級電通訊中,時間的轉換問題顯得格外突出。整個地球分為24個時區,每個時區都有自己的本地時間。為了統一起見,使用一個統一的時間,稱為通用協調時(Universal Time Coordinated,UTC),是由世界時間標準設定的全球標準時間
PHP預設的時區設定是UTC時間,而北京位於時區的東8區,領先UTC8個小時,所以在使用time()等函數時,並不能得到正確的時間。可以通過以下兩種方式來修改時區
1、修改設定檔
如果使用的是獨立的伺服器,有許可權修改設定檔,設定時區就可以通過修改php.ini中的date.timezone屬性完成,可以將這個屬性的值設定為"Asia/Shang"、"Asia/Chongqing"、"Etc/GMT-8"或"PRC"中的一個
2、調用函數
可以通過調用函數date_default_timezone_set()函數來設定時區
date_default_timezone_set(‘PRC‘);
<?php echo date("Y-m-d H:i:s")."<br>";//2017-02-28 07:06:05 date_default_timezone_set(‘PRC‘); echo date("Y-m-d H:i:s")."<br>";//2017-02-28 14:06:05?>
date_default_timezone_get()函數可以用來擷取當前的預設時區
<?php echo date_default_timezone_get();//Europe/Paris date_default_timezone_set(‘PRC‘); echo date_default_timezone_get();//PRC?>
微秒
在PHP中,大多數的時間格式都是以UNIX時間戳記來表示的,而UNIX時間戳記是心秒為最小的計量時間的單位。這對於某些應用程式來說不夠準確,所以可以調用microtime()返回當前UNIX時間戳記和微秒數
mixed microtime ([ bool $get_as_float ] )
如果給出了 get_as_float 參數並且其值等價於 TRUE,microtime() 將返回一個浮點數
<?php echo microtime() ."<br>";//0.72119500 1488282853 echo microtime(true) ;//1488282853.7212?>
microtime()函數常用於效能分析
<?php date_default_timezone_set("PRC"); $start = microtime(true); for($i=0; $i<100000; $i++); $end = microtime(true); echo $end-$start;//0.0067892074584961?>
擷取時間
前面介紹的date()函數用於設定時間,而getdate()函數則主要用於擷取時間
array getdate ([ int $timestamp = time() ] )
該函數將根據timestamp得出的包含有日期資訊的關聯陣列array。如果沒有給出時間戳記則認為是當前本地時間
返回的關聯陣列中的鍵名單元有以下幾個
鍵名 說明 傳回值例子"seconds" 秒的數字表示 0 到 59"minutes" 分鐘的數字表示 0 到 59"hours" 小時的數字表示 0 到 23"mday" 月份中第幾天的數字表示 1 到 31"wday" 星期中第幾天的數字表示 0 (周日) 到 6 (周六)"mon" 月份的數字表示 1 到 12"year" 4 位元字表示的完整年份 1999 或 2003"yday" 一年中第幾天的數字表示 0 到 365"weekday" 星期幾的完整文本表示 Sunday 到 Saturday"month" 月份的完整文本表示, January 或 March January 到 December0 自從 Unix 紀元開始至今的秒數,和 time() 的傳回值以及用於 date() 的值類似
<?phpdate_default_timezone_set(‘PRC‘);//Array ( [seconds] => 8 [minutes] => 4 [hours] => 20 [mday] => 28 [wday] => 2 [mon] => 2 [year] => 2017 [yday] => 58 [weekday] => Tuesday [month] => February [0] => 1488283448 )print_r(getdate())."<br>";//Array ( [seconds] => 0 [minutes] => 0 [hours] => 0 [mday] => 1 [wday] => 6 [mon] => 1 [year] => 2000 [yday] => 0 [weekday] => Saturday [month] => January [0] => 946656000 )print_r(getdate(strtotime(‘2000-1-1 0:0:0‘)));?>
日曆
下面使用物件導向的寫法,完成一個簡單的日曆控制項
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>body{margin: 0;}.active{color:white;background-color: blue;}table{text-align:center;table-layout:fixed;border-collapse:collapse;width:400px;}</style></head><body><?php class Calendar{ private $year; //當前年 private $month; //當前月 private $start_weekday; //當前月的第一天對應的周幾 private $days; //當前月總天數 private $out; //用於構造日曆 //構造方法,用來初始化一些日期屬性 function __construct(){ //如果使用者沒有設定年份,則使用當前系統時間的年份 $this->year = isset($_GET[‘year‘]) ? $_GET[‘year‘] : date(‘Y‘); //如果使用者沒有設定月份,則使用當前系統時間的月份 $this->month = isset($_GET[‘month‘]) ? $_GET[‘month‘] : date(‘m‘); //利用date()函數的w參數擷取當前月第一天對應周幾 $this->start_weekday = date("w", mktime(0,0,0, $this->month, 1, $this->year)); //利用date()函數的t參數擷取當前月的天數 $this->days = date("t", mktime(0,0,0, $this->month, 1, $this->year)); } //魔術方法,用於列印整個日曆 function __tostring(){ global $out; $out .=‘<table>‘;//日曆以表格形式列印 $out .=$this->changeDate();//使用者自己設定日期 $out .=$this->weeksList();//列印周列表 $out .=$this->daysList();//列印日列表 $out .=‘</table>‘;//表格結束 return $out; } //輸出周列表 private function weeksList(){ global $out; $week = array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘); $out .= ‘<tr>‘; for($i = 0; $i < count($week); $i++){ $out .= ‘<th>‘ .$week[$i] .‘</th>‘; } $out.= ‘</tr>‘; } //輸出日列表 private function daysList(){ global $out; $out .= ‘<tr>‘; //輸出空格 for($j = 0; $j < $this->start_weekday; $j++){ $out .= ‘<td> </td>‘; } //輸出日期 for($k = 1; $k <= $this->days; $k++){ $j++; if($k == date(‘d‘)){ $out .= ‘<td class="active">‘ .$k .‘</td>‘; }else{ $out .= ‘<td>‘.$k .‘</td>‘; } //換行 if($j % 7 == 0){ $out .= ‘</tr><tr>‘; } } //補齊空格 while($j % 7 !== 0){ $out .= ‘<td> </td>‘; $j++; } $out.= ‘</tr>‘; } //處理上一年資料 private function prevYear($year,$month){ $year = $year - 1; if($year < 1970){ $year = 1970; } return "year={$year}&month={$month}"; } //處理上一月資料 private function prevMonth($year,$month){ if($month == 1){ $year = $year - 1; if($year < 1970){ $year = 1970; } $month = 12; }else{ $month --; } return "year={$year}&month={$month}"; } //處理下一年資料 private function nextYear($year,$month){ $year = $year + 1; if($year > 2038){ $year = 2038; } return "year={$year}&month={$month}"; } //處理下一月資料 private function nextMonth($year,$month){ if($month == 12){ $year = $year + 1; if($year < 1970){ $year = 1970; } $month = 1; }else{ $month ++; } return "year={$year}&month={$month}"; } //改變日期 private function changeDate($url="1.php"){ global $out; $out .=‘<tr>‘; $out .=‘<td><a href="‘ .$url .‘?‘ .$this->prevYear($this->year,$this->month) .‘">‘ .‘前一年‘ .‘</a></td>‘; $out .=‘<td><a href="‘ .$url .‘?‘ .$this->prevMonth($this->year,$this->month) .‘">‘ .‘前一月‘ .‘</a></td>‘; $out .= ‘<td colspan="3">‘; $out .=‘<form>‘; $out .=‘<select name="year" onchange="window.location=\‘‘.$url.‘?year=\‘+this.options[selectedIndex].value+\‘&month=‘.$this->month.‘\‘">‘; for($sy=1970;$sy<=2038;$sy++){ $selected = ($sy==$this->year) ? "selected" : ""; $out .=‘<option ‘.$selected .‘ value="‘ .$sy .‘">‘ .$sy .‘</option>‘; }; $out .= ‘</select>‘; $out .= ‘<select name="month" onchange="window.location=\‘‘.$url.‘?year=‘.$this->year.‘&month=\‘+this.options[selectedIndex].value">‘; for($sm=1;$sm<=12;$sm++){ $selected1 = ($sm==$this->month) ? "selected" : ""; $out .=‘<option ‘.$selected1 .‘ value="‘ .$sm .‘">‘ .$sm .‘</option>‘; } $out .= ‘</select>‘; $out .= ‘</form>‘; $out .= ‘</td>‘; $out .= ‘<td><a href="‘.$url.‘?‘.$this->nextYear($this->year,$this->month).‘">‘.‘後一年‘.‘</a></td>‘; $out .= ‘<td><a href="‘.$url.‘?‘.$this->nextMonth($this->year,$this->month).‘">‘.‘後一月‘.‘</a></td>‘; $out .= ‘</tr>‘; } } echo new Calendar();?> </body></html>
php日期時間函數