php日期时间函数使用笔记!

2021-03-24  本文已影响0人  DragonersLi
 $date = '2020-02-20';
$date = $date ? $date : date("Y-m-d");
$firstday = date('Y-m-01', strtotime($date));//当月首日
date('Y-m-d', strtotime($firstday. ' +1 month -1 day')); //当月末
date('Y-m-01', strtotime(date('Y-m-d', strtotime($date)) . ' -1 month'));//上月首日
date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . '  -1 day'));//上月末日
 $firstday = date("Y-m-01",time());//月初 
 $lastday = date("Y-m-".date('t'));//月末  
 $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));//月末  
 
date('Y/m/d',strtotime('-1 day')) //昨天
date('Y/m/d')    //今天
date('Y/m/d',strtotime('+1 day')) //明天
 

如果为正+号可省略:1 year ; 12 month ; 365 days;54 weeks
$this_year_first_day =  date('Y-01-01');//今年第一天
$this_year_last_day = date('Y-12-31');//今年最后一天
$this_year_last_day = date('Y-m-d',strtotime($this_year_first_day." +1 year -1 day"));//今年最后一天

$last_year_first_day = date('Y-01-01',strtotime($this_year_first_day ." - 1 year"));//去年第一天
$last_year_last_day = date('Y-m-d',strtotime($this_year_first_day ."-1 day"));//去年最后一天

$next_year_first_day = date('Y-01-01',strtotime($this_year_first_day ." + 1 year"));//明年第一天
$next_year_last_day = date('Y-m-d',strtotime($this_year_first_day ." + 2 years -1 day"));//明年最后一天



date('Y').'/'.(ceil((date('n'))/3)*3-3+1).'/01' //本季初
date('Y').'/'.(ceil((date('n'))/3)*3).'/'.date('t',mktime(0,0,0,(ceil((date('n'))/3)*3),1,date('Y'))) #本季末

$n = 0;//当$n为0时,为当前季度,为-1为上一季度,为正一为下一季度,以此类推
$this_quarter_first_day =date('Y-m-01',mktime(0,0,0,(ceil(date('n') /3)+$n - 1) *3 +1,1,date('Y')));
$this_quarter_last_day =date('Y-m-t',mktime(0,0,0,(ceil(date('n') /3)+$n) * 3,1,date('Y'))); 
$n = 0;//当$n为0时,为当前周,为-1为上周,为正一为下周,以此类推
date('Y-m-d', strtotime("this week {$n} week"));周一
date('Y-m-d', strtotime("this week {$n+1} week -1 day"));周日

date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)) //这周周一 
date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)) //这周周日


$this_week_first_day = date('Y-m-d', strtotime('this week')); //本周一 
$this_week_last_day = date('Y-m-d', strtotime(' next week -1 day')); //本周日 
$last_week_first_day = date('Y-m-d', strtotime(' last week')); //上周一 
$last_week_last_day = date('Y-m-d', strtotime(' this week -1 day')); //上周日 
$next_week_first_day = date('Y-m-d', strtotime(' next week')); //下周一


       function timeTran($time)
    {
        $t = time() - $time;
        $f = array(
            '31536000' => '年',
            '2592000' => '个月',
            '604800' => '星期',
            '86400' => '天',
            '3600' => '小时',
            '60' => '分钟',
            '1' => '秒'
        );
        foreach ($f as $k => $v) {
            if (0 != $c = floor($t / (int)$k)) {
                return $c . $v . '前';
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读