php 获取最近一周,一个月,一年

Posted 阿波罗任

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 获取最近一周,一个月,一年相关的知识,希望对你有一定的参考价值。

<?php

//PRC是中国的意思,这段代码是把默认时区设置成了中国标准时间。
date_default_timezone_set(‘PRC‘);
/**
* 获取最近一周,一个月,一年
* */
function getLatelyTime($type = ‘‘){
$now = time();
$result = [];
if($type == ‘week‘){
//最近一周
for($i=0;$i<7;$i++){
$result[] = date(‘Y-m-d‘,strtotime(‘-‘.$i.‘ day‘, $now));
}
}elseif($type == ‘month‘){
//最近一个月
for($i=0;$i<30;$i++){
$result[] = date(‘Y-m-d‘,strtotime(‘-‘.$i.‘ day‘, $now));
}
}elseif($type == ‘year‘){
//最近一年
for($i=0;$i<12;$i++){
$result[] = date(‘Y-m‘,strtotime(‘-‘.$i.‘ month‘, $now));
}
}
return $result;
}


echo ‘<pre>‘;
print_r(getLatelyTime(‘year‘));

以上是关于php 获取最近一周,一个月,一年的主要内容,如果未能解决你的问题,请参考以下文章