php获取时间的常用函数,常用php时间函数用法汇总

一周 86400*7=604800秒

set_time_limit用法:set_time_limit(秒数);规定从该句运行时起程序必须在指定秒数内运行结束, 超时则程序出错退出.

一个问题举例

$nowdate="1999-08-05" ;

$aa=getdate($nowdate);

$year=$aa['year'];

$month=$aa['mon'];

echo $year."";

echo $month;

为何得到:

1970

1

我希望得到:

1999

8

如何解决?

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

$nowdate="1999-08-05" ;

$aa=strtotime($nowdate);

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

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

echo $year."";

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."
";

echo date("Y-m-d H:i:s")."
";

$now_T=mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));

echo "此时的time".$now_T."
";

$now_S=mktime("$d3","$d2","$d1","$d5","$d4","$d6");

echo "2004-09-09 18:10:00时的time".$now_S."
";

$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; //间隔天数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值