您的位置:首页 > 编程语言 > PHP开发

php日期时间计算,转载

2016-08-23 08:33 295 查看
//php获取今天日期
date("Y-m-d");
//php获取昨天日期
date("Y-m-d",strtotime("-1 day"))
//php获取明天日期
date("Y-m-d",strtotime("+1 day"))
//php获取一周后日期
date("Y-m-d",strtotime("+1 week"))
//php获取一周零两天四小时两秒后时间
date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds"))
//php获取下个星期四日期
date("Y-m-d",strtotime("next Thursday"))
//php获取上个周一日期
date("Y-m-d",strtotime("last Monday"))
//php获取一个月前日期
date("Y-m-d",strtotime("last month"))
//php获取一个月后日期
date("Y-m-d",strtotime("+1 month"))
//php获取十年后日期
date("Y-m-d",strtotime("+10 year"))
//php获取今天起止时间戳
mktime(0,0,0,date('m'),date('d'),date('Y'));
mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//php获取昨天起止时间戳
mktime(0,0,0,date('m'),date('d')-1,date('Y'));
mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
//php获取上周起止时间戳
mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
//php获取本月起止时间戳
mktime(0,0,0,date('m'),1,date('Y'));
mktime(23,59,59,date('m'),date('t'),date('Y'));

计算起止日期(列出起止日期区间所有日期)
$data=$this->date_range(date('Y-m-d',$time2),date('Y-m-d',$time1));
//起止日期计算
function date_range($first, $last, $step = '+1 day', $format = 'Y-m-d')
{
$dates   = array();
$current = strtotime($first);
$last    = strtotime($last);

while ($current <= $last) {
$dates[] = date($format, $current);
$current = strtotime($step, $current);
}
return $dates;
}

/**
* 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
* @param type $time
* @parma key 传入加减的月份数字
* @return type
*/
function last_month_today($time,$key=''){
$last_month_time = mktime(date("G", $time), date("i", $time),
date("s", $time), date("n", $time), 0, date("Y", $time));
$last_month_t =  date("t", $last_month_time);

if ($last_month_t < date("j", $time)) {
return date("Y-m-t H:i:s", $last_month_time);
}
if(isset($key)&&!empty($key)){
return date(date("Y-m",strtotime("-".$key. "month")) . "-d", $time);
}else{
return date(date("Y-m", $last_month_time) . "-d", $time);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息