PHP日期函数汇总

今日开始时间戳和结束时间戳

public static function today()
{
    return [
        'start' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
        'end' => mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1,
    ];
}

昨日开始时间戳和结束时间戳

public static function yesterday()
{
    return [
        'start' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
        'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
    ];
}

最近五天开始时间戳和结束时间戳

public static function five()
{
    return [
        'start' => mktime(0, 0, 0, date('m'), date('d') - 6, date('Y')),
        'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
    ];
}

最近七天开始时间戳和结束时间戳

public static function seven()
{
    return [
        'start' => mktime(0, 0, 0, date('m'), date('d') - 7, date('Y')),
        'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
    ];
}

最近30日开始时间戳和结束时间戳

public static function thirty()
{
    return [
        'start' => mktime(0, 0, 0, date('m'), date('d') - 30, date('Y')),
        'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
    ];
}

这周开始时间戳和结束时间戳

public static function thisWeek()
{
    $length = 0;
    // 星期天直接返回上星期,因为计算周围 星期一到星期天,如果不想直接去掉
    if (date('w') == 0) {
        $length = 7;
    }

    return [
        'start' => mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - $length, date('Y')),
        'end' => mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - $length, date('Y')),
    ];
}

本月开始时间戳和结束时间戳

public static function thisMonth()
{
    return [
        'start' => mktime(0, 0, 0, date('m'), 1, date('Y')),
        'end' => mktime(23, 59, 59, date('m'), date('t'), date('Y')),
    ];
}

今年开始时间戳和结束时间戳

public static function thisYear()
{
    $start_month = 1;
    $end_month = 12;

    $start_time = date('Y') . '-' . $start_month . '-1 00:00:00';
    $end_month = date('Y') . '-' . $end_month . '-1 23:59:59';
    $end_time = date('Y-m-t H:i:s', strtotime($end_month));

    return [
        'start' => strtotime($start_time),
        'end' => strtotime($end_time)
    ];
}

上个月开始时间戳和结束时间戳

public static function lastMonth()
{
    $start = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
    $end = mktime(23, 59, 59, date('m') - 1, date('t'), date('Y'));

    if (date('m', $start) != date('m', $end)) {
        $end -= 60 * 60 * 24;
    }

    return [
        'start' => $start,
        'end' => $end,
    ];
}

几个月前开始时间戳和结束时间戳

public static function monthsAgo($month)
{
    return [
        'start' => mktime(0, 0, 0, date('m') - $month, 1, date('Y')),
        'end' => mktime(23, 59, 59, date('m') - $month, date('t'), date('Y')),
    ];
}

某年开始时间戳和结束时间戳

public static function aYear($year)
{
    $start_month = 1;
    $end_month = 12;

    $start_time = $year . '-' . $start_month . '-1 00:00:00';
    $end_month = $year . '-' . $end_month . '-1 23:59:59';
    $end_time = date('Y-m-t H:i:s', strtotime($end_month));

    return [
        'start' => strtotime($start_time),
        'end' => strtotime($end_time)
    ];
}

某月开始时间戳和结束时间戳

public static function aMonth($year = 0, $month = 0)
{
    $year = $year ?? date('Y');
    $month = $month ?? date('m');
    $day = date('t', strtotime($year . '-' . $month));

    return [
        "start" => strtotime($year . '-' . $month),
        "end" => mktime(23, 59, 59, $month, $day, $year)
    ];
}

获取两时间戳相差的时间

public static function timediff($begin_time, $end_time)
{
    if ($begin_time < $end_time) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    //计算天数
    $timediff = $endtime - $starttime;
    $days = intval($timediff / 86400);
    //计算小时数
    $remain = $timediff % 86400;
    $hours = intval($remain / 3600);
    //计算分钟数
    $remain = $remain % 3600;
    $mins = intval($remain / 60);
    //计算秒数
    $secs = $remain % 60;

    $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);

    return $res;
}

获取本年所有月份,开始和结束时间戳

public static function getMonth()
{
   $year = date('Y');
   $yeararr = [];
   $month = [];
   for ($i=1; $i <=12 ; $i++) {
       $yeararr[$i] = $year.'-'.$i;
   }
   foreach ($yeararr as $key => $value) {
       $timestamp = strtotime( $value );
       $start_time = date( 'Y-m-1 00:00:00', $timestamp );
       $mdays = date( 't', $timestamp );
       $end_time = date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp );

       $month[$key]['start_time'] = $start_time;
       $month[$key]['end_time'] = $end_time;

   }
   return $month;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值