php開發之時間跟日期的應用

來源:互聯網
上載者:User
php開發之時間和日期的應用

1,比較兩個日期的大小
比較兩個日期的大小,首先是將日期轉換為時間戳記,然後對時間戳記進行比較。
具體範例程式碼如下:

$time1 =date("Y-m-d H:i:s");$time2 ="2009-12-9 4:40:12";echo "時間變數\$time1 的時間為:".$time1."\n";echo "時間變數\$time2 的時間為:".$time2."\n";if(Strtotime($time1)-strtotime($time2)<0){    echo "\$time1早於\$time2";}else{    echo "\$time2早於\$time1";}?>

運行結果如下:

2,實現頁面指令碼啟動並執行時間
這裡主要使用microtime()函數來實現,該函數返回當前UNIX時間戳記和微秒數。返回格式為msec sec的字串。其中sec為當前的UNIX時間戳記,msec為微秒數。
文法格式如下:
string microtime(void)

範例程式碼如下:

function run_time(){    list($msec,$sec) =explode(" ",microtime());  //使用explode()函數返回兩個變數    return((float)$msec + (float)$sec);          //返回兩個變數的和}$start =run_time();  //第一次運行run_time()函數for($i=0;$i<100000;$i++){}$end =run_time();    //再一次運行run_time()函數echo "啟動並執行時間:".($end-$start)."\n";?>

  • 聯繫我們

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