<?php

namespace App\Utils;

use Illuminate\Support\Facades\Log;

class DateTimeUtils
{
    /**
     * 今天7点到明天7点算作今天
     */
    public static function getToday()
    {
        $time = time();
        $inventoryTime = strtotime(date('Y-m-d 07:00:00'));
        if ($time < $inventoryTime) {
            $time = strtotime('-1 day');
        }

        return date('Y-m-d', $time);
    }

    public static function getNextDay()
    {
        $time = time();
        $inventoryTime = strtotime(date('Y-m-d 07:00:00'));
        if ($time > $inventoryTime) {
            $time = strtotime('+1 day');
        }

        return date('Y-m-d', $time);
    }

    public static function getMicroTime($dateTime = null)
    {
        $time = microtime(true);
        if ($dateTime && is_string($dateTime)) {
            $time = strtotime($dateTime);
        }
        if ($dateTime && is_int($dateTime)) {
            $time = $dateTime;
        }
        $time *= 1000;

        return (int)ceil($time);
    }


    public static function validateDate($date, $format = 'Y-m-d')
    {
        $d = \DateTime::createFromFormat($format, $date);
        return $d && $d->format($format) === $date;
    }

    public static function excelUploadDateToString($excelData, $defaultTime, $format = "Y-m-d")
    {

        try {
            $time = ($excelData - 25569) * 24 * 3600;
            if ($format == "Y-m-d H:i:s") {
                $time = $time - 8 * 3600;
            }
            return date($format, $time);
        } catch (\Exception $exception) {
            Log::error("时间转化出错", [$exception->getMessage()]);
        }
        return $defaultTime;

    }

}
