php获取本年、本月、本周时间戳和日期格式

38 篇文章 1 订阅
35 篇文章 2 订阅

设置时区

date_default_timezone_set("Asia/Shanghai"); 
date_default_timezone_set('PRC');//这两种方法效果相同

时间戳转日期,可以用date(‘Y-m-s h:i:s’, 具体时间戳来实现)
日期转换时间戳,用strtotime(“date()”).

时间戳格式

//指定日期
$date = strtotime($hang_date); 
//指定日期的上月的 1号
$firstday = date ( 'Y-m-01', strtotime ( date ( 'Y', $date ) . '-' . (date ( 'm', $date ) - 1) . '-01' ) );
//指定日期的上月最后一天
$lastday = date ( 'Y-m-d', strtotime ( "$firstday +1 month -1 day" ) ); 
//指定日期的月的 1号
$this_firstday = date ( 'Y-m-01', strtotime ( date ( 'Y', $date ) . '-' . (date ( 'm', $date ) ) . '-01' ) );
//指定日期的月最后一天
$this_lastday = date ( 'Y-m-d', strtotime ( "$this_firstday +1 month -1 day" ) );  

//获取今日开始时间戳和结束时间戳  
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));  
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;  

//获取昨日起始时间戳和结束时间戳  
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));  
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;  

//获取本周起始时间戳和结束时间戳   
$beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));  
$endThisweek=time();  

//获取上周起始时间戳和结束时间戳  
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));  
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));  

//获取下周起始时间戳和结束时间戳  
$beginnextweek=date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1+7,date("Y")));
$endnextweek=date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7+7,date("Y")));

//获取本月起始时间戳和结束时间戳  
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));  
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));  

 //上个月的起始时间:  
$begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));  
$end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));  

$begin_year = strtotime(date("Y",time())."-1"."-1"); //本年开始  
$end_year = strtotime(date("Y",time())."-12"."-31"); //本年结束  

//现在的时间到第二天凌晨相差的时间戳  
$time = (strtotime(date('Y-m-d'))+3600*24) - time() ; 

日期格式

echo '<br>上周起始时间:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
echo '<br>本周起始时间:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";

echo '<br>上月起始时间:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
echo '<br>本月起始时间:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";
//本年起始
echo date(‘Y-01-01); 
//结束日期 
echo date(‘Y-12-31);
//本周一
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); //本月一日减一天即是上月最后一日

	//今天开始时间
        $beginToday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')));
        //今天结束时间
        $endToday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
        //昨天开始时间
        $beginYesterday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-1,date('Y')));
        //昨天结束时间
        $endYesterday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
        //本周开始时间
        $beginThisweek = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
        //本周结束时间
        $endThisweek = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
        //上周开始时间
        $beginLastweek= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
        //上周结束时间
        $endLastweek= date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
        //本月开始时间
        $beginThismonth = date("Y-m-d H:i:s",mktime(0,0,0,date('m'),1,date('Y')));
        //本月结束时间
        $endThismonth = date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('t'),date('Y')));
        //上月开始时间
        $beginLastmonth = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
        //上月结束时间
        $endLastmonth = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
        //本季度开始时间
        $season = ceil((date('n'))/3);//当前是第几季度
        $beginThisseason = date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
        //本季度结束时间
        $endThisseason = date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')));
        //上季度开始时间
        $season1 = ceil((date('n'))/3)-1;//上季度是第几季度
        $beginLastseason = date('Y-m-d H:i:s', mktime(0, 0, 0,$season1*3-3+1,1,date('Y')));
        //上季度结束时间
        $endLastseason = date('Y-m-d H:i:s', mktime(23,59,59,$season1*3,date('t',mktime(0, 0 , 0,$season1*3,1,date("Y"))),date('Y')));
        //本年开始时间
        $beginThisyear=date("Y",time())."-1"."-1";
        //本年结束时间
        $endThisyear =date("Y",time())."-12"."-31";

PHP 获取今日、昨日、本周、上周、本月的等等常用的起始时间戳和结束时间戳的时间处理类

<?php
class Time
{
    /**
     * 返回今日开始和结束的时间戳
     *
     * @return array
     */
    public static function today()
    {
        return [
            mktime(0, 0, 0, date('m'), date('d'), date('Y')),
            mktime(23, 59, 59, date('m'), date('d'), date('Y'))
        ];
    }
 
    /**
     * 返回昨日开始和结束的时间戳
     *
     * @return array
     */
    public static function yesterday()
    {
        $yesterday = date('d') - 1;
        return [
            mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
            mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
        ];
    }
 
    /**
     * 返回本周开始和结束的时间戳
     *
     * @return array
     */
    public static function week()
    {
        $timestamp = time();
        return [
            strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))),
            strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1
        ];
    }
 
    /**
     * 返回上周开始和结束的时间戳
     *
     * @return array
     */
    public static function lastWeek()
    {
        $timestamp = time();
        return [
            strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
            strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
        ];
    }
 
    /**
     * 返回本月开始和结束的时间戳
     *
     * @return array
     */
    public static function month($everyDay = false)
    {
        return [
            mktime(0, 0, 0, date('m'), 1, date('Y')),
            mktime(23, 59, 59, date('m'), date('t'), date('Y'))
        ];
    }
 
    /**
     * 返回上个月开始和结束的时间戳
     *
     * @return array
     */
    public static function lastMonth()
    {
        $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
        $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));
 
        return [$begin, $end];
    }
 
    /**
     * 返回今年开始和结束的时间戳
     *
     * @return array
     */
    public static function year()
    {
        return [
            mktime(0, 0, 0, 1, 1, date('Y')),
            mktime(23, 59, 59, 12, 31, date('Y'))
        ];
    }
 
    /**
     * 返回去年开始和结束的时间戳
     *
     * @return array
     */
    public static function lastYear()
    {
        $year = date('Y') - 1;
        return [
            mktime(0, 0, 0, 1, 1, $year),
            mktime(23, 59, 59, 12, 31, $year)
        ];
    }
 
    public static function dayOf()
    {
 
    }
 
    /**
     * 获取几天前零点到现在/昨日结束的时间戳
     *
     * @param int $day 天数
     * @param bool $now 返回现在或者昨天结束时间戳
     * @return array
     */
    public static function dayToNow($day = 1, $now = true)
    {
        $end = time();
        if (!$now) {
            list($foo, $end) = self::yesterday();
        }
 
        return [
            mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
            $end
        ];
    }
 
    /**
     * 返回几天前的时间戳
     *
     * @param int $day
     * @return int
     */
    public static function daysAgo($day = 1)
    {
        $nowTime = time();
        return $nowTime - self::daysToSecond($day);
    }
 
    /**
     * 返回几天后的时间戳
     *
     * @param int $day
     * @return int
     */
    public static function daysAfter($day = 1)
    {
        $nowTime = time();
        return $nowTime + self::daysToSecond($day);
    }
 
    /**
     * 天数转换成秒数
     *
     * @param int $day
     * @return int
     */
    public static function daysToSecond($day = 1)
    {
        return $day * 86400;
    }
 
    /**
     * 周数转换成秒数
     *
     * @param int $week
     * @return int
     */
    public static function weekToSecond($week = 1)
    {
        return self::daysToSecond() * 7 * $week;
    }
 
    private static function startTimeToEndTime()
    {
 
    }
}
 
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值