简体   繁体   English

如何在php中将ISO8601转换为日期格式

[英]How to convert ISO8601 to Date format in php

How to convert this ( in ISO8601 format ): 2014-03-13T09:05:50.240Z如何转换( ISO8601 格式): 2014-03-13T09:05:50.240Z

To this ( in MySQL DATE format ): 2014-03-13对此( MySQL DATE 格式): 2014-03-13

in php?在 PHP 中?

try this尝试这个

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

The complete date function documentation can be found here: http://php.net/manual/en/function.date.php完整的日期函数文档可以在这里找到:http: //php.net/manual/en/function.date.php

The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp. PHP 函数“strtotime”除了将时间字符串转换为 unix 时间戳之外什么也不做。

Hope I could help :)希望我能帮忙:)

Ps: Just in case strtotime will return 0 try using this: Ps:以防万一 strtotime 将返回 0 尝试使用:

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime(substr($date,0,10)));

Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):从 PHP 5.2.0开始,您也可以使用OOPDateTime()来实现(当然,如果您更喜欢 OOP):

$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d');    // MySQL datetime format

There is no reason to use the inefficient time functions.没有理由使用低效的时间函数。 The most efficient way is to simply extract the first 10 characters:最有效的方法是简单地提取前 10 个字符:

substr($date,0,10)

People, that are really coding for year ≥10000, can use:真正编码 ≥10000 年的人可以使用:

substr($date,0,strpos($date,"T"))

Simply convert datetime description into a Unix timestamp using with strtotime and then five format using Date Formats只需使用strtotime将 datetime 描述转换为 Unix 时间戳,然后使用Date Formats将五种格式

Try it will surely work for you.试试它肯定会为你工作。

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

For those using Carbon (php library), the parse() works quite well:对于那些使用 Carbon(php 库)的人, parse()工作得很好:

Carbon::parse($date)

https://carbon.nesbot.com/docs/ https://carbon.nesbot.com/docs/

Today I have published an interitty/utils package that deals with, among other things, the ISO-8601 format and perhaps all permutations of this standard.今天,我发布了一个interitty/utils包,其中处理 ISO-8601 格式以及可能该标准的所有排列。

I hope it will help you too.我希望它也能帮助你。

$dateTimeFactory = new Interitty\Utils\DateTimeFactory();
$dateTime = $dateTimeFactory->createFromIso8601('1989-12-17T12:00:00Z');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM