<?php

namespace time;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/8/10
 * Time: 19:16
 */
class Timestamp
{
    /**
     * 获取毫秒级别的时间戳
     * @return float
     */
    public static function getMillisecond()
    {
        list($t1, $t2) = explode(' ', microtime());
        $time = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
        return $time;
    }

    /**
     * 获取今日开始时间戳
     * https://www.cnblogs.com/wyl0514/p/11007695.html
     * mktime(hour,minute,second,month,day,year)
     */
    public static function beginToday()
    {
        //$todaystart = strtotime(date('Y-m-d'.'00:00:00',time()));
        return mktime(0, 0, 0, date('m'), date('d'), date('Y'));
    }

    /**
     * 获取今日结束时间戳
     * @return false|int
     */
    public static function endToday()
    {
        //$todayend = strtotime(date('Y-m-d'.'00:00:00',time()+3600*24));
        return mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
    }

    /**
     * 获取昨日开始时间戳
     * @return false|int
     */
    public static function beginYesterday()
    {
        return mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
    }

    /**
     * 获取昨日结束时间戳
     * @return false|int
     */
    public static function endYesterday()
    {
        return mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
    }

    /**
     * 获取指定范围内每天的起始结束时间戳
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function dayTimeRangeList($startTime, $endTime)
    {
        $begin = self::dayStart($endTime);
        $arr = [];
        $arr[] = [$begin, $endTime];
        while (($begin = strtotime('-1 day', $begin)) >= $startTime) {
            $arr[] = [$begin, self::dayEnd($begin)];
        }
        if (end($arr)[0] > $startTime) {
            $arr[] = [$startTime, self::dayEnd($begin)];
        }
        return $arr;
    }

    /**
     * 时间戳 转 当日起始时间戳
     * @param $timestamp
     * @return false|int
     */
    public static function dayStart($timestamp)
    {
        return strtotime(date('Y-m-d 00:00:00', $timestamp));
    }

    /**
     * 时间戳 转 当日结束时间戳
     * @param $timestamp
     * @return false|int
     */
    public static function dayEnd($timestamp)
    {
        return strtotime(date('Y-m-d 23:59:59', $timestamp));
    }
}