【问题标题】:Parse date format in laravel carbon在 laravel carbon 中解析日期格式
【发布时间】:2021-11-30 18:03:06
【问题描述】:

我正在尝试使用 laravel carbon 解析日期格式。这里我试过了

dd(Carbon::parse('2021-10-12T09:20:27:915 GMT+0600')->format('Y-m-d h:i:s'));

它显示 DateTime::__construct(): Failed to parse time string (2021-10-12T09:20:27:915 GMT+0600) at position 19 (:): Unexpected

提前致谢

【问题讨论】:

  • 您在哪里/如何获得这种格式的日期?

标签: laravel laravel-5


【解决方案1】:

在上面的代码中,您已经提到,您正在使用 DateTime 和 Locale。

 /**
     * Create a carbon instance from a string.
     *
     * This is an alias for the constructor that allows better fluent syntax
     * as it allows you to do Carbon::parse('Monday next week')->fn() rather
     * than (new Carbon('Monday next week'))->fn().
     *
     * @param string|DateTimeInterface|null $time
     * @param DateTimeZone|string|null      $tz
     *
     * @throws InvalidFormatException
     *
     * @return static
     */
    public static function parse($time = null, $tz = null)
    {
        $function = static::$parseFunction;

        if (!$function) {
            return static::rawParse($time, $tz);
        }

        if (\is_string($function) && method_exists(static::class, $function)) {
            $function = [static::class, $function];
        }

        return $function(...\func_get_args());
    }

你可以使用而不是使用 parse

    public static function parseFromLocale($time, $locale = null, $tz = null)

您将能够非常轻松地分析您的时间。

【讨论】:

    猜你喜欢
    • 2023-01-05
    • 2016-12-01
    • 1970-01-01
    • 2018-04-20
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2023-02-07
    相关资源
    最近更新 更多