php 输出时间差,PHP输出时间差函数示例

电玩女神 2022-11-17 03:43 246阅读 0赞

这篇文章主要为大家详细介绍了PHP输出时间差函数示例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随512笔记的小玲来看看吧!

PHP输出时间差函数

代码如下:

/* 512笔记 www.512Pic.com */

date_default_timezone_set(‘PRC’); //默认时区

echo “今天:”,date(“Y-m-d”,time()),”
“;

echo “今天:”,date(“Y-m-d”,strtotime(“18 june 2008”)),”
“;

echo “昨天:”,date(“Y-m-d”,strtotime(“-1 day”)), “
“;

echo “明天:”,date(“Y-m-d”,strtotime(“+1 day”)), “
“;

echo “一周后:”,date(“Y-m-d”,strtotime(“+1 week”)), “
“;

echo “一周零两天四小时两秒后:”,date(“Y-m-d G:H:s”,strtotime(“+1 week 2 days 4 hours 2 seconds”)), “
“;

echo “下个星期四:”,date(“Y-m-d”,strtotime(“next Thursday”)), “
“;

echo “上个周一:”.date(“Y-m-d”,strtotime(“last Monday”)).”
“;

echo “一个月前:”.date(“Y-m-d”,strtotime(“last month”)).”
“;

echo “一个月后:”.date(“Y-m-d”,strtotime(“+1 month”)).”
“;

echo “十年后:”.date(“Y-m-d”,strtotime(“+10 year”)).”
“;

?>

在学习PHP 的时候,经常会用到获取现在之前或之后,某个时间段的日期。现在已经进行收集,大家同时也可以进行扩展丰富

代码如下:

//获取当天的星期(1-7)

function GetWeek($times)

{

$res = date(‘w’, strtotime($times));

if($res==0)

$res=7;

return $res;

}

//获取当天时间

function GetTime($times)

{

$res = date(‘H:i’, strtotime($times));

return $res;

}

//获取现在过几月的的时间

function GetMonth($Month,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$Month months”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$Month months”));

return $res;

}

//获取当前时间

function GetCurrentDateTime()

{

$res=date(“Y-m-d H:i:s”,time());

return $res;

}

//获取当前时间隔几小时之前或之后的时间

function GetDiffHours($hours,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$hours hour”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$hours hour”));

return $res;

}

//间隔几分钟之前或之后的时间

function GetDiffMinute($Minute,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$Minute minute”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$Minute minute”));

return $res;

}

//间隔几秒之前或之后的时间

function GetDiffSec($sec,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$sec second”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$sec second”));

return $res;

}

//间隔几个星期之前或之后的时间

function GetDiffWeek($Week,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$Week week”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$Week week”));

return $res;

}

// 间隔几天之间的时间

function GetDiffDays($days,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$days day”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$days day”));

return $res;

}

//间隔几年之前或之后的时间

function GetDiffYears($year,$type=’l’)

{

if(!strcmp($type,’b’))

$res=date(“Y-m-d H:i:s”,strtotime(“-$year year”));

if(!strcmp($type,’l’))

$res=date(“Y-m-d H:i:s”,strtotime(“+$year year”));

return $res;

}

注:关于PHP输出时间差函数示例的内容就先介绍到这里,更多相关文章的可以留意512笔记的其他信息。

关键词:

您可能感兴趣的文章

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP数组---输出

      和数组的创建一样,数组的输出显示也有三种方法。   一。for循环   突然发现PHP一个比较有趣的地方,就是它用$定义变量后,不管走到哪里,只要使用变量,变量名前面都

    相关 php输出多维数组函数

      php数组的定义与数组遍历,php数组函数的用法与例子,php数组取值赋值,php多维数组的循环输出等,供大家学习参考。   1、php数组定义和遍历   2、php数

    相关 php输出语句

    php输出语句 常见的输出语句 echo(): 可以一次输出多个值,多个值之间用逗号分隔。echo是语言结构(language construct),而并不