PHP时间戳代码 上周一等:
$week_this_monday =strtotime('last Monday');//本周一
$tomorrow =strtotime("+1 day");//明天
$week_last_monday = strtotime('last Monday') -3600*24*7;//上周一
$week_last_sunday =strtotime('last Monday')-3600*24;//上周日
$ptime=date("Y-m-d",$week_last_sunday);//转换一下
echo$ptime;
php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期:
<?php
//本周一
echo date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); //w为星期几的数字形式,这里0为周日
//本周日
echo date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)); //同样使用w,以现在与周日相关天数算
//上周一
echo date('Y-m-d', strtotime('-1 monday', time())); //无论今天几号,-1 monday为上一个有效周未
//上周日
echo date('Y-m-d', strtotime('-1 sunday', time())); //上一个有效周日,同样适用于其它星期
//本月一日
echo date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00')); //直接以strtotime生成
//本月最后一日
echo date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
//t为当月天数,28至31天
//上月一日
echo date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
//本月一日直接strtotime上减一个月
//上月最后一日
echo date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);
//本月一日减一天即是上月最后一日
<?php
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'));
/*
时间戳转换成日期不用说了
但是日期要转成时间戳的话就要用到
strtotime()
*/
$time = time();//时间戳
$nowtime = date('Y-m-d H:i:s', $time);//生成带格式的日期
$oldtime ='2010-11-10 22:19:21';
$catime = strtotime($oldtime);//日期转换为时间戳
$nowtimes = date('Y-m-d H:i:s', $catime);//时间戳又转回日期了
echo$nowtimes;