各种时间操作(PHP)

一、获取时间

1、获取本周一
date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); //w为星期几的数字形式,这里0为周日
2、获取本周日
date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)); //同样使用w,以现在与周日相关天数算
3、获取上周一
date('Y-m-d', strtotime('-1 monday', time())); //无论今天几号,-1 monday为上一个有效周未
4、获取上周日
date('Y-m-d', strtotime('-1 sunday', time())); //上一个有效周日,同样适用于其它星期
5、获取本月一日
date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00')); //直接以strtotime生成
6、获取本月最后一日
date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00')); //t为当月天数,28至31天
7、获取上月一日
date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00'))); //本月一日直接strtotime上减一个月
8、获取上月最后一日
date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400); //本月一日减一天即是上月最后一日
9、获取当前时间前6天
date('Y-m-d', strtotime('-6 days'));
10、指定时间加一个月
date("Y-m-d",strtotime("+1 month",strtotime("2012-02-04")));
11、指定时间加一周
date("Y-m-d",strtotime("+1 week",strtotime("2011-02-04")));
12、指定时间加一天
date("Y-m-d",strtotime("+1 day",strtotime("2011-02-04")));
13、当前时间加一天
date('Y-m-d H:i:s',strtotime('+1 day'));
14、当前时间加一小时
date('Y-m-d H:i:s',strtotime('+1 hour'));
15、当前时间加一分钟
date('Y-m-d H:i:s',strtotime('+1 minute'));
16、当前时间加一月
date('Y-m-d H:i:s',strtotime('+1 mouth'));
17、获取当前月的第一天和最后一天
$date = date('Y-m-d');
function getMonth($date){
      $firstday = date("Y-m-01",strtotime($date));
      $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
      return array($firstday,$lastday);
}
18、获取上月的第一天和最后一天
$date = date('Y-m-d');
function getlastMonthDays($date){
     $timestamp=strtotime($date);
     $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
     return array($firstday,$lastday);
 }
19、获取下月的第一天和最后一天
$date = date('Y-m-d');
function getNextMonthDays($date){
    $timestamp=strtotime($date);
    $arr=getdate($timestamp);
    if($arr['mon'] == 12){
        $year=$arr['year'] +1;
        $month=$arr['mon'] -11;
        $firstday=$year.'-0'.$month.'-01';
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    }else{
        $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    }
    return array($firstday,$lastday);
}

二、计算指定时间和当前时间相差年月日时分秒

date_default_timezone_set('PRC');//声明时区
$enddate="2018-6-14 17:29:10";
$date=floor((strtotime($enddate)-strtotime(date('Y-m-d H:i:s')))/86400);
echo "相差天数:".$date."天<br/><br/>";
$hour=floor((strtotime($enddate)-strtotime(date('Y-m-d H:i:s')))%86400/3600);
echo "相差小时数:".$hour."小时<br/><br/>";
$minute=floor((strtotime($enddate)-strtotime(date('Y-m-d H:i:s')))%86400/60);
echo "相差分钟数:".$minute."分钟<br/><br/>";
$second=floor((strtotime($enddate)-strtotime(date('Y-m-d H:i:s')))%86400%60);
echo "相差秒数:".$second."秒";

三、指定时间戳 加指定秒,分钟,小时等

1、当前时间戳 格式:2019-03-13 18:00:00 echo date('Y-m-d H:i:s', strtotime('now')); 
2、当前时间戳+1秒 echo date('Y-m-d H:i:s', strtotime('+1second'));
3、当前时间戳+1分 echo date('Y-m-d H:i:s', strtotime('+1minute'));
4、当前时间戳+1小时 echo date('Y-m-d H:i:s', strtotime('+1hour'));
5、当前时间戳+1天 echo date('Y-m-d H:i:s', strtotime('+1day'));
6、当前时间戳+1周 echo date('Y-m-d H:i:s', strtotime('+1week'));
7、当前时间戳+1月 echo date('Y-m-d H:i:s', strtotime('+1month'));
8、当前时间戳+1年 echo date('Y-m-d H:i:s', strtotime('+1year'));
9、当前时间戳+12年,12月,12天,12小时,12分,12秒 echo date('Y-m-d H:i:s', strtotime('+12year 12month 12day 12hour 12minute 12second'));


$t = 1483967416;$t是指定时间戳
10、指定时间戳 echo $date = date('Y-m-d H:i:s', $t);
10.1、指定时间戳+1天 echo date('Y-m-d H:i:s', $t+1*24*60*60);
10.2、指定时间戳+1天 echo date('Y-m-d H:i:s', strtotime("+1day", $t));
11、指定时间戳+1年 echo date('Y-m-d H:i:s', $t+365*24*60*60);
11.1、指定时间戳+1年 echo date('Y-m-d H:i:s', strtotime("+1year", $t));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值