这篇文章主要介绍了php简单获取上月、本月、近15天、近30天的方法,结合实例形式分析了php通过自定义函数封装的日期与时间戳转换相关运算技巧,需要的朋友可以参考下
具体如下:
/**
* 获取统计时间
* @param $type
* 1 上月
* 2 本月
* 3 近15天
* 4 近30天
* @return array
*/
function getDateInfo($type)
{
$data = array(
array(
'firstday' => date('Ym01', strtotime('-1 month')),
'lastday' => date('Ymt', strtotime('-1 month')),
),
array(
'firstday' => date('Ym01', strtotime(date("Y-m-d"))),
'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")),
),
array(
'firstday' => date('Ymd', strtotime("-15 day")),
'lastday' => date('Ymd', strtotime('-1 day')),
),
array(
'firstday' => date('Ymd', strtotime("-30 day")),
'lastday' => date('Ymd', strtotime('-1 day')),
),
);
return is_null($type) ? $data : $data[$type-1];
}
print_r(getDateInfo(1));//获取上个月第一天与最后一天立即学习“PHP免费学习笔记(深入)”;
运行结果:
Array ( [firstday] => 20170601 [lastday] => 20170630 )
立即学习“PHP免费学习笔记(深入)”;
相关推荐:
立即学习“PHP免费学习笔记(深入)”;
以上就是PHP实现获取上月、本月、近15天、近30天的方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号