常用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时间函数及用法:
$month
, int $day
, int $year
)Day
的值在给定的 month
所应该具有的天数范围之内,闰年已经考虑进去了;year 的值是从 1 到 32767。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")
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".
$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')
$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
$timestamp
= time() ] )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
$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
gmdate: 取得目前与 GMT 差后的时间。
easter_date: 计算复活节日期。
easter_days: 计算复活节与三月廿一日之间日期数。
gmmktime: 取得 UNIX 时间戳记的格林威治时间。
一个问题举例
$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);
return (bool)FALSE;
}
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获得指定日期所在星期的第一天和最后一天
$lastday = date('Y-m-d', strtotime("$day Sunday"));
$firstday = date('Y-m-d', strtotime("$lastday -6 days"));
return array($firstday, $lastday);
}
* @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);
mysql日期时间函数 http://www.jb51.net/article/23966.htm
posted on 2017-02-23 10:40 Ryanyanglibin 阅读(1766) 评论(0) 编辑 收藏 举报