PHP 获取时间并显示

1.获取系统当前时间

echo "date('Y-m-d',time())";

2.获取系统前一天时间
echo "date("Y-m-d",strtotime("-1 day"))";
需要前几天就减去(-) 需要几天后就加(+)
3.获取当前系统时间上一周周一和周日 日期
//上周周日的日期
$lastSunday = date('Y-m-d', strtotime('-1 sunday', time()));

//根据时间差 减去6天前,算出上周周一日期
$lastMonday = date('Y-m-d',strtotime('$lastSunday -6 day',$ss));
4.获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$today = date('Y-m-d',time());

//获取当前周的第几天 周日是 0 周一到周六是 1 - 6  
$w=date('w',strtotime($today));  
$first=1;

//获取本周开始日期,如果$w是0,则表示周日,减去 6 天     
$weekStart=date('Y-m-d 00:00:00',strtotime("$today-".($w ? $w - $first : 6).' days'));            

//本周结束日期  
$weekEnd=date('Y-m-d 23:59:59',strtotime("$weekStart +6 days"));

//前一周的起始时间
$weekStart = date('Y-m-d 00:00:00',strtotime("$weekStart -7 days"));
$weekEnd = date('Y-m-d 23:59:59',strtotime("$weekEnd -7 days")););
5.上一个月一共多少天
$t = date('t',strtotime('-1 month'));

6. php获取三个月前的日期

<?php header("content-Type: text/html; charset=utf-8");?>
<?php
$s_sdate=date("Y-m-d"); //当前时间
$moth_day=90; //月份 (转为天数)
$s_edate=date("Y-m-d",(strtotime($s_sdate)-$moth_day*84600));
echo $moth_day."前的日期为".$s_edate;
?>

7. 计算日期30天后

$t = time(); // 当前时间戳
$t = strtotime("+30 days", $t); // 30天后的时间戳
echo date("Y-m-d", $t); // 格式化日期

8. 转换2日期的时间戳...然后相减

$t1 = strtotime("2009-08-19");
$t2 = strtotime("2009-08-20");
$t = $t2 - $t1; // 相差天数的秒
echo (int)($t / 86400)

9. 判断是否是本星期

$date = "2008-12-08";
if (isCurrentWeeks($date)) {
  echo $date."是本星期";
} else {
  echo $date."不是本星期";
}
function isCurrentWeeks($d) {
  return (date("W",strtotime($d))==date("W",strtotime("now")));
}

<?php
//今天
$today = date("Y-m-d");
//昨天
$yesterday = date("Y-m-d", strtotime(date("Y-m-d"))-86400);
//上周
$lastweek_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));
$lastweek_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")));
//本周
$thisweek_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))); 
$thisweek_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))); 
//上月
$lastmonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))); 
$lastmonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))); 
//本月
$thismonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))); 
$thismonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))); 
//本季度未最后一月天数 
$getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));
//本季度/
$thisquarter_start = date('Y-m-d H:i:s', mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y'))); 
$thisquarter_end = date('Y-m-d H:i:s', mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y')));
 
 
//2016-08-10这天 2个月后的日期
echo date("Y-m-d",strtotime("+2 month",strtotime("2016-08-10")));
     
//当前 3个月后的日期
echo date("Y-m-d",strtotime("+3 month",time()));

猜你喜欢

转载自blog.csdn.net/LQZ8888/article/details/90727385