【问题标题】:How to convert date from XML file [duplicate]如何从 XML 文件转换日期 [重复]
【发布时间】:2022-02-18 19:03:56
【问题描述】:

我从2019-05-13T16:02:16. 中以下列方式找到的 XML 文件中获取日期

在 PHP 变量中获取此日期后,我将其保存在 SQL 中的表中,以便为日期提供正确的格式并且只有日期这样存储而没有时间,我正在执行以下操作:

$xml = new SimpleXMlElement( $_FILES['XmlToUpload']['tmp_name'], 0, true );
$fechaCadena = strtotime("21/05/2021");
$fechaEntrada = getdate($fechaCadena);
$fechaEntrada = $xml['Fecha'];

查看表格中的日期时,以如下格式保存:

2019-05-13

我想要将此日期转换为以下格式:

dd/mm/yyyy

我之前尝试通过replace'-' 更改为'/',但还是不行

$fechaCadena = strtotime(str_replace('-', '/',"21/05/2021"));

我希望有人就如何正确地将日期转换为我想要的格式给我一些指导。

【问题讨论】:

    标签: php xml date


    【解决方案1】:

    如果我正确理解您的问题,您可以在从 XML 文件中获取时间后简单地使用以下代码:

    $format = date("d/m/Y", strtotime("2019-05-13")); //You can put a variable instead of 2019-05-13
    echo $format;
    

    导致:

    13/05/2019
    

    根据您的 cmets,您也有兴趣将日期保存到 SQL 表中。据我所知,SQL 以时间戳的格式保存 DateTime。如果您有兴趣以所需格式获取日期,则应使用SELECT 语句和CONVERT 函数,如link 中所示。

    【讨论】:

    • 在签入数据库时​​,它将以以下格式保存 2019-05-13。在将其保存到数据库之前我还需要做些什么吗?
    • @user11804298 你的数据库是什么类型的? MySQL?如果您使用的是 SQL 并且列类型是 VAR 我认为您应该没有任何问题。
    • 我的类型数据库是SQL Server 2008 R2,列类型是datetime
    • @user11804298 虽然我不确定这是否足够,但我编辑了我的答案。
    【解决方案2】:

    使用 DateTimeImmutable 类。

    $date = new DateTimeImmutable(
        '2019-05-13', 
        new DateTimezone('America/Los_Angeles')
    );
    

    现在您可以使用format() 方法以特定格式输出日期:

    var_dump($date->format('m/d/Y'));
    
    string(10) "05/13/2019"
    

    您可能想知道为什么我提供了时区。好吧,试试以下方法:

    // full date time in iso format
    var_dump(
        $date->format(DateTimeInterface::RFC3339)
    );
    
    // with a different timezone
    var_dump(
       $date
         ->setTimezone(new DateTimezone('America/Atka'))
         ->format(DateTimeInterface::RFC3339)
    );
    
    string(25) "2019-05-13T00:00:00-07:00"
    string(25) "2019-05-12T22:00:00-09:00"
    

    完整的日期时间需要完整的时区。

    另一种格式化日期时间的方法是使用IntlDateFormatter。它允许您使用语言环境设置日期格式,从而更轻松地开发多种语言。

    var_dump(
        [
            'en-US'=> IntlDateFormatter::formatObject(
                $date, [IntlDateFormatter::SHORT, IntlDateFormatter::NONE], 'en-US'
            ),
            'en-GB'=> IntlDateFormatter::formatObject(
                $date, [IntlDateFormatter::SHORT, IntlDateFormatter::NONE], 'en-GB'
            ),
            'de-DE'=> IntlDateFormatter::formatObject(
                $date, [IntlDateFormatter::SHORT, IntlDateFormatter::NONE], 'de-DE'
            ),
            'ar-AE'=> IntlDateFormatter::formatObject(
                $date, [IntlDateFormatter::SHORT, IntlDateFormatter::NONE], 'ar-AE'
            ),
        ]
    );
    
    array(3) {
      ["en-US"]=>
      string(7) "5/13/19"
      ["en-GB"]=>
      string(10) "13/05/2019"
      ["de-DE"]=>
      string(8) "13.05.19"
      ["ar-AE"]=>
      string(22) "١٣‏/٥‏/٢٠١٩"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2023-04-08
      • 1970-01-01
      • 2014-03-23
      • 2011-05-23
      • 2014-03-04
      • 1970-01-01
      相关资源
      最近更新 更多