PHP時間處理函數

來源:互聯網
上載者:User
  1. //mktime()
  2. 將日期和時間轉換為unix時間戳記
  3. //time()
  4. 擷取目前時間的unix時間戳記
  5. echo date("Y-m-d",mktime(0,0,0,12,31,2013))."
    ";
  6. //執行個體:通過計算兩個unix時間戳記的差,來計算一個使用者的年齡
  7. $year = 1991;
  8. //假設使用者的出生日期是1991.07.16
  9. $month = 07;
  10. $day = 16;
  11. $brithday = mktime(0,0,0,$month,$day,$year);
  12. //將使用者的出生日期轉換為unix時間戳記
  13. $nowdate = time();
  14. //獲得目前時間的unix時間戳記
  15. $ageunix = $nowdate - $brithday;
  16. //擷取時間戳記的差值
  17. $age = floor($ageunix / (60*60*24*365));
  18. //時間戳記的差值除以每年的秒數即是使用者的實際年齡
  19. echo "該使用者的年齡是".$age."

    ";
  20. //date_default_timezone_set()
  21. 設定時區
  22. //getdate()
  23. 確定當前的時間
  24. //gettimeofday()
  25. 擷取某一天中的具體時間
  26. //date_sunrise()
  27. 某天的日出時間
  28. //date_sunset()
  29. 某天的日落時間
  30. //date()
  31. 格式化一個本地時間和日期
  32. //microtime()
  33. 返回當前UNIX時間戳記和微秒數
  34. //下面的類通過獲得兩次函數的執行時間,來計算程式的執行時間
  35. class Timer{
  36. private $startTime;
  37. private $stopTime;
  38. function __construct(){
  39. $this->startTime = 0;
  40. $this->stopTime = 0;
  41. }
  42. function start(){
  43. $this->startTime = microtime(true);
  44. }
  45. function stop(){
  46. $this->stopTime = microtime(true);
  47. }
  48. function spent(){
  49. return round(($this->startTime - $this->stopTime),4);
  50. }
  51. }
  52. $timer = new Timer();
  53. $timer->start();
  54. usleep(1000);
  55. $timer->stop();
  56. echo "執行指令碼用時".$timer->spent()."秒";
  57. ?>
複製代碼
PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.