2014年1月27日 星期一

php的time()、date()、strtotime()、mktime()初步認識

int time(void):

表示從Unix紀元(1970/01/01 00:00:00)開始到現在的秒數。

傳回值為:int。





string date(string $format[, int $timestamp]):

格式化本地的時間或日期。

第一個參數($format)是格式化字串,詳細請參考:

http://tw2.php.net/manual/en/function.date.php

第二個參數為時間戳($timestamp),預設是time(),可省略。

傳回值為:string。如果參數錯誤,則回傳false。





int strtotime(string $time [, int $now = time() ]):

將英文文字轉換成時間戳(timestamp)。

第一個參數是一個英文字串,php官網範例如下:

<?php
    echo strtotime("now"), "\n";
    echo strtotime("10 September 2000"), "\n";
    echo strtotime("+1 day"), "\n";
    echo strtotime("+1 week"), "\n";
    echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
    echo strtotime("next Thursday"), "\n";
    echo strtotime("last Monday"), "\n";

?>

第二個參數恕時間戳(timestamp),預設是time(),可省略。

傳回值是:int。如果參數錯誤,則回傳false(php 5.1版之前傳回 -1)。






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 ]]]]]]] ):

取得一個日期的Unix時間戳。

第一個參數:小時數。

第二個參數:分鐘數。

第三個參數:秒數。

第四個參數:月份數。

第五個參數:天數。

第六個參數:年數。

第七個參數:當值為1時,表示為夏令時間(Daylight saving time)。0則為非。

(php 5.1.0 以後的版本已取消第七個參數)

參數可以從右向左省略,或者說由後向前省略。

EX:

mktime(0, 0, 0, 2); //省略了天數和年數(還有夏令時間)。

傳回值:int。如果參數錯誤,則回傳false(php 5.1版之前傳回 -1)。

沒有留言:

張貼留言