php时间差 秒,PHP输出时间差函数代码

本文详细介绍了在PHP中处理日期和时间的多种实用函数,包括获取当前日期、指定时区、计算时间差以及定制未来或过去特定时间点。涵盖了日期格式化、星期数获取、精确时间调整等功能,是PHP开发者必备的时间管理参考。
摘要由CSDN通过智能技术生成

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

PHP输出时间差函数

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值