1. 程式人生 > >錯誤處理和時間函數

錯誤處理和時間函數

中華 bsp com 所有 default 註意 def 時區 但是

錯誤處理和時間函數

一、錯誤處理

a) 錯誤報告級別

  1. 語法錯誤: error 會給一個致命錯誤 終止程序繼續執行
  2. 運行時錯誤: notice warning 運行代碼的時候錯了 有錯誤提示,但是他們不會影響程序運行 但是結果不是我們想要的
  3. 邏輯錯誤:邏輯出現錯誤 最大的難就 就是不報錯 不好排除
  4. notice: 本身不是一個錯誤 只是一個提示 這個錯誤可以忽略
  5. warning: 警告只要產生warning錯誤 程序的執行結果就不是我們想要的,但是這個級別的錯誤,不會終止程序執行 但是這個錯誤必須解決掉
  6. error:致命錯誤 必須排除

b) 調整錯誤報告級別

E_ERROR 1 致命的運行時錯誤(阻止代碼執行)

E_WARNING 2 運行時警告

E_NOTICE 8 運行時註意

E_ALL 所有的錯誤 警告的註意信息

c) trigger_error 代替die()

trigger_error 可以模擬一個報錯信息輸出

d) 自定義錯誤處理

1.屏蔽錯誤

1.

ini_set() 設置php.ini中的配置項

ini_get() 獲取php.ini中的配置項

2.

error_reporting() 設置錯誤報告級別

E_ALL^E_NOTICE

E_ALL^E_NOTICE^E_WARNING

3.手動修改錯誤

a) ; Possible Values:

b) ; Off = Do not display any errors

c) ; stderr = Display errors to STDERR (affects only CGI/CLI binaries!)

d) ; On or stdout = Display errors to STDOUT

e) ; Default Value: On

f) ; Development Value: On

g) ; Production Value: Off

h) ; http://php.net/display-errors

i) display_errors = On 將on 改成off屏蔽錯誤

或者

; Common Values:

; E_ALL (Show all errors, warnings and notices including coding standards.)

; E_ALL & ~E_NOTICE (Show all errors, except for notices)

; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.)

; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)

; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

; Development Value: E_ALL

; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT

; http://php.net/error-reporting

error_reporting =E_ALL & ~E_NOTICE

2.自定義錯誤日誌

error_log() 使用指定的文件記錄錯誤報告日誌

error_log寫入wamp下面的php日誌中

; Log errors to specified file. PHP‘s default behavior is to leave this value

; empty.

; http://php.net/error-log

; Example:

error_log ="c:/wamp/logs/php_error.log"

; Log errors to syslog (Event Log on Windows).

;error_log = syslog

3.以下幾種情況可以考慮自定義錯誤處理

a) 可以記下錯誤信息 及時發現一些生產環境出現的問題

b) 可以屏蔽錯誤

c) 可以控制錯誤的輸出

d) 可以作為調試工具

二、時間函數

  1. UNIX 時間戳

自從Unix紀元(格林威治時間1970年1月1日 00:00:00)到現在的秒數

  1. 獲取事件戳

time() 函數返回一個當前系統的時間戳

  1. 格式化時間戳

date() 有兩個參數 第一個參數是要以什麽格式輸出 第二個參數是時間戳

返回將整數時間戳 按照給定的格式字符串而產生字符串 如果沒有給出時間戳則使用本地當前時間戳 換句話來說就是時間戳可選 如果沒有就默認當前時間戳

  1. 獲取指定時間的時間戳

mktime取的一個日期的unix時間戳

mktime(時,分,秒,月,日,年)

  1. 時區

date_default_timezone_set() 設置時區

參數值:

PRC 中華人民共和國

Asia/Shanghai 亞洲/上海

Asia/ChongQing 亞洲/重慶

Asia/Hong_Kong 亞洲/香港

date_default_timezone_get() 獲取時區

  1. 用英文文本來描述時間

strtotime() 將任何英文文本的日期和時間描述成時間戳

//unix時間戳的有效期

//2147483647

echo ‘<br/>‘;

echo date(‘Y-m-d H:i:s‘,‘2147483647‘);

錯誤處理和時間函數