常用php時間函數用法匯總


1.設置時區的方法:

php5后都要自己設置時區,要么修改php.ini的設置,要么在代碼里修改。 

在PHP.INI中設置時區

date.timezone = PRC

在代碼中設置時區

1 date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai'   亞洲/上海

2 date_default_timezone_set('Asia/Chongqing');//其中Asia/Chongqing'為“亞洲/重慶”

3 date_default_timezone_set('PRC');//其中PRC為“中華人民共和國”

4 ini_set('date.timezone','Etc/GMT-8');

5 ini_set('date.timezone','PRC');

6 ini_set('date.timezone','Asia/Shanghai');

7 ini_set('date.timezone','Asia/Chongqing');

2.php時間函數及用法:

checkdate: 驗證日期的正確性。
bool checkdate ( int  $month , int  $day , int  $year )
month 的值是從 1 到 12, Day 的值在給定的  month 所應該具有的天數范圍之內,閏年已經考慮進去了;year 的值是從 1 到 32767。
 
 
date: 將服務器的時間格式化。
string date ( string $format [, int $timestamp ] ):返回將整數  timestamp 按照給定的格式字串而產生的字符串。如果沒有給出時間戳則使用本地當前時間。換句話說, timestamp 是可選的,默認值為  time()
date("Y-m-d H:i:s", strtotime("-1 days")) //昨天此刻的時間,明天則是 "1 days",前天則是"-2 days";
 
          
date("Y-m-d H:i:s", strtotime("0 days")) //今天此刻的時間,等價於date("Y-m-d H:i:s")
U 替換成從一個起始時間(好象是1970年1月1日)以來的秒數

Y 替換成4位的年號.
y 替換成2位的年號.
F 替換成月份的英文全稱.
M 替換成月份的英文簡稱.
m 替換成月份數.
z 替換成從當年1月1日以來的天數.
d 替換成日數.
l 替換成星期幾的英文全稱.
D 替換成星期幾的英文簡稱.
w 替換成星期幾(數字).
H 替換成小時數(24小時制).
h 替換成小時數(12小時制).
i 替換成分鍾數.
s 替換成秒數.
A 替換成"AM"或"PM".
a 替換成"am"或"pm".
S 替換成序數字后綴,例如:"st","nd","rd","th".

time: 取得目前時間的 UNIX 時間戳記。
int time ( void )返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到當前時間的秒數。
$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d H:i:s') ."\n";
echo 'Next Week: '. date('Y-m-d H:i:s', $nextWeek) ."\n";
// or using strtotime():
echo 'Next Week: '. date('Y-m-d H:i:s', strtotime('+1 week')) ."\n";
echo 'Previous Week: '. date('Y-m-d H:i:s', strtotime('-1 week')) ."\n";//或者用strtotime('-7 day')
 
mktime: 取得 UNIX 時間戳記。
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 時間戳。時間戳是一個長整數,包含了從 Unix 紀元(January 1 1970 00:00:00 GMT)到給定時間的秒數。如果參數非法,本函數返回  FALSE(在 PHP 5.1 之前返回  -1)。
$lastday = mktime(0, 0, 0, 3, 0, 2000);//括號里的參數分別表示 時分秒月天年
echo strftime("Last day in Feb 2000 is: %d", $lastday)."<br>";
$lastday = mktime(0, 0, 0, 4, -31, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
輸出:
Last day in Feb 2000 is: 29
Last day in Feb 2000 is: 29
 
getdate: 獲得時間及日期信息。
array getdate ([ int  $timestamp = time() ] )
$today = getdate();
var_dump($today);
輸出
 
          
array (size=11)
  'seconds' => 
int
 27
  'minutes' => 
int
 44
  'hours' => 
int
 8
  'mday' => 
int
 4
  'wday' => 
int
 3
  'mon' => 
int
 11
  'year' => 
int
 2015
  'yday' => 
int
 307
  'weekday' => 
string
 'Wednesday' (length=9)
  'month' => 
string
 'November' (length=8)
  0 => 
int
 1446597867
 
microtime: 取得目前時間的 UNIX 時間戳記的百萬分之一秒值。
mixed microtime ([ bool  $get_as_float ] )如果給出了$ get_as_float 參數並且其值等價於 TRUE,該函數將返回一個浮點數。
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

// Sleep for a while
usleep(10000000);//usleep — 以指定的微秒數延遲執行,10000000是10秒,sleep是延遲多少秒

$time_end = microtime_float();
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";//Did nothing in 9.9999949932098 seconds
 
strftime: 將服務器的時間本地格式化。http://php.net/manual/zh/function.strftime.php 
 
gettimeofday: 取得目前時間。 http://php.net/manual/zh/function.gettimeofday.php

gmdate: 取得目前與 GMT 差后的時間。

easter_date: 計算復活節日期。

easter_days: 計算復活節與三月廿一日之間日期數。

gmmktime: 取得 UNIX 時間戳記的格林威治時間。

  
3.補充:
$_SERVER['REQUEST_TIME'] 是 PHP 內置的當前頁面開始運行時的時間戳,在當前頁面運行結束時將 time() - $_SERVER['REQUEST_TIME'] 得到的就是當前頁面運行的時間(秒):
 
一天86400秒 
一周 86400*7=604800秒
 
set_time_limit用法:set_time_limit(秒數);規定從該句運行時起程序必須在指定秒數內運行結束, 超時則程序出錯退出.

一個問題舉例
$nowdate="1999-08-05" ;

$aa=getdate($nowdate);

$year=$aa['year'];

$month=$aa['mon'];

echo $year."</br>";

echo $month;

為何得到:

1970

1

我希望得到:

1999

8

如何解決?
--------------------------------------------------------------------------------
$nowdate="1999-08-05" ;

$aa=strtotime($nowdate);

$year=date("Y",$aa);

$month=date("n",$aa);

echo $year."</br>";

echo $month;

--------------------------------------------------------------------------------

$endtime="2004-09-09 18:10:00";
$d1=substr($endtime,17,2); //秒
$d2=substr($endtime,14,2); //分
$d3=substr($endtime,11,2); // 時
$d4=substr($endtime,8,2); //日
$d5=substr($endtime,5,2); //月
$d6=substr($endtime,0,4); //年

echo $d1.'-'.$d2.'-'.$d3.'-'.$d5.'-'.$d4.'-'.$d6."<br>";
echo date("Y-m-d H:i:s")."<br>";
$now_T=mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
echo "此時的time".$now_T."<br>";
$now_S=mktime("$d3","$d2","$d1","$d5","$d4","$d6");
echo "2004-09-09 18:10:00時的time".$now_S."<br>";
$end_TS=($now_T-$now_S)/60; //計算 剩余分鍾
echo "相差".$end_TS."分";
輸出
00-10-18-09-09-2004
2015-11-04 09:02:08
此時的time1446598928
2004-09-09 18:10:00時的time1094724600
相差5864572.1333333分
 

常用時間函數封裝:
/**
 * 將秒數轉換為時間(年、天、小時、分、秒)
 * @param int $time  秒數比如86400
 * @return bool|string
 */
public static function Sec2Time($time)
{
    if (is_numeric($time)) {
        $value = array(
            "years" => 0, "days" => 0, "hours" => 0,
            "minutes" => 0, "seconds" => 0,
        );
        if ($time >= 31556926) {
            $value["years"] = floor($time / 31556926);
            $time = ($time % 31556926);
        }
        if ($time >= 86400) {
            $value["days"] = floor($time / 86400);
            $time = ($time % 86400);
        }
        if ($time >= 3600) {
            $value["hours"] = floor($time / 3600);
            $time = ($time % 3600);
        }
        if ($time >= 60) {
            $value["minutes"] = floor($time / 60);
            $time = ($time % 60);
        }
        $value["seconds"] = floor($time);
        $t = $value["years"] . "年" . $value["days"] . "天" . " " . $value["hours"] . "小時" . $value["minutes"] . "分" . $value["seconds"] . "秒";
        return $t;
    } else {
        return (bool)FALSE;
    }
}
 
/**
 *當前日期是本月的第幾周
 * @param int $date Y-m-d格式的時間,下同
 * @return int 
 
 * /
function weekOfMonth($date) {
    $firstOfMonth = strtotime(date("Y-m-01", strtotime ($date)));
    return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
}
 
//獲取指定日期所在月的第一天和最后一天
function GetTheMonth($date)
{
    $firstday = date("Y-m-01", strtotime($date));
    $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
    return array($firstday, $lastday);
}

//PHP獲得指定日期所在星期的第一天和最后一天
function getdays($date )
{
    $lastday = date('Y-m-d', strtotime("$day Sunday"));
    $firstday = date('Y-m-d', strtotime("$lastday -6 days"));
    return array($firstday, $lastday);
}

/**
 * 獲得指定日期的前/后$step(正負表示指定日期所在月的前后的第幾個月份)個月的第一天和最后一天日期 
 * @param $date
 * @param $step
 * @return string
 */
function AssignTabMonth($date, $step)
{
    $date = date("Y-m-d", strtotime($step . " months", strtotime($date)));//得到處理后的日期(得到前后月份的日期)
    $firstday = date("Y-m-01", strtotime($date));
    $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
    return array($firstday, $lastday);
}
 
$date = date('Ymd', strtotime($date));
$lastday = date('Ymd', strtotime("$date Sunday"));//周日
$firstday = date('Ymd', strtotime("$lastday -6 days"));//周一
 
$firstday = date('Ymd', strtotime(date('Y-m-01', strtotime($date))));//指定日期所在月的第一天
$lastday = date("Ymd", strtotime("$firstday +1 month -1 day"));//指定日期所在月的最后一天
$dayCount = $lastday - $firstday + 1; //間隔天數
mysql日期時間函數 http://www.jb51.net/article/23966.htm 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM