Home > Enterprise >  How to convert date from XML file
How to convert date from XML file

Time:02-18

I am getting a date from an XML file which is found in the following way in the 2019-05-13T16:02:16.

After obtaining this date in a PHP variable I save it in a table in SQL, to give the date a correct format and that only the date is stored like this without time, I am doing the following:

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

When reviewing the date in the table, it is saved in the following format:

2019-05-13

What I would like is to convert this date into the following format:

dd/mm/yyyy

I tried before by replace changing the '-' by '/' but it still didn't work

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

I would like someone to give me a little more guidance on how to correctly convert the date to the format I want.

CodePudding user response:

If I understand your problem correctly, you can simply use below code after getting the time from XML file:

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

which results in:

13/05/2019

As per your comments, you are also interested in saving the date into a SQL table. From what I know, SQL save DateTime in the format of Timestamps. If you are interested in getting the date in the desired format, you should use SELECT statement and CONVERT function like what is shown in this link.

CodePudding user response:

Use the DateTimeImmutable class.

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

Now you can use the format() method to output the date in a specific format:

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

You might wonder why I provided a time zone. Well, try the following:

// 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"

A full date time needs a time zone to be complete.

Another possibility to format a date time is to use the IntlDateFormatter. It allows you to format a date using a locale, making it a lot easier to develop for multiple languages.

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) "١٣‏/٥‏/٢٠١٩"
}
  • Related