php時間函數的用法及執行個體分析

來源:互聯網
上載者:User
這篇文章主要介紹了php時間函數用法,結合執行個體形式分析了php時間戳記相關函數time,mktime,date及strtotime的使用方法,非常簡單實用,需要的朋友可以參考下

php中有unix時間戳記的 相關操作函數,使用很方便

time() 返回當前的 Unix 時間戳記

microtime -- 返回當前 Unix 時間戳記和微秒數

例 1. 用 microtime() 對指令碼的運行計時

<?php/*** Simple function to replicate PHP 5 behaviour*/function microtime_float(){  list($usec, $sec) = explode(" ", microtime());  return ((float)$usec + (float)$sec);}$time_start = microtime_float();// Sleep for a whileusleep(100);$time_end = microtime_float();$time = $time_end - $time_start;echo "Did nothing in $time seconds/n";?>

mktime()取得一個日期的 Unix 時間戳記

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

參數可以從右向左省略,任何省略的參數會被設定成本地日期和時間的當前值

date()格式化一個本地時間/日期

string date ( string format [, int timestamp] )

提示: 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中儲存了發起該請求時刻的時間戳記。

strtotime -- 將任何英文文本的日期時間描述解析為 Unix 時間戳記

echo strtotime("+1 day"), "/n";echo strtotime("+1 week"), "/n";

例2. 某個時間的後一天,後一月

strtotime("+1 day ".$day);strtotime("2008-01-31 +1 month");strtotime($day." +1 day");

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.