常用php时间函数用法汇总

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