0

I'm pretty new to php and have a problem with the following function:

protected function convertDate($date)
  {
    $toExplode = $date;
    $pos = strpos($date, 'T');

    if ($pos !== false) {
      $toExplode = substr_replace($toExplode, ' ', $pos, 1);
    }

    if (strpos($toExplode, '.') !== false) {
      $exploded = explode('.', $toExplode);
      return $exploded[0];
    }

    if (strpos($toExplode, '+') !== false) {
      $exploded = explode('+', $toExplode);
      return $exploded[0];
    }
  }

My inputs have these formats:

  • 2019-05-26T22:01:00.000Z
  • 2019-03-11 10:58:33.979+00

But I would like my output to have this format:

  • 2019-03-11 10:58:33

Unfortunately, when I apply the function, it returns null for this date: 2019-04-23T16:26:59Z

I can't figure out what's wrong with my function.

Coder1
  • 25
  • 4
  • The term "sanitize" is slightly ambiguous. Do you want to _accept_ or _reject_ alternative formats? The former should be straightforward since both `new \DateTime()` and `strtotime()` will happily accept anything that loosely resembles a date; the latter can be trickier. – Álvaro González May 31 '21 at 08:06
  • Does this answer your question? [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – El_Vanja May 31 '21 at 08:18
  • You're not looking to sanitize anything, you just want to convert the format. – El_Vanja May 31 '21 at 08:19
  • I have updated the title – Coder1 May 31 '21 at 08:29

2 Answers2

0

You can use the core functions from PHP to do this, without having to explode/implode/replace

strotime converts any known Date/Time format into UNIX Timestamp. Documentation

date allows you to reconvert the UNIX Timestamp into any readable format. Documentation

protected function convertDate($date) {
    $time = strtotime($date);
    return date("Y-m-d H:i:s", $time);
}

A whole example code with the 2 sample dates can be found below:

$date1 = '2019-05-26T22:01:00.000Z';
$date2 = '2019-03-11 10:58:33.979+00';
function convertDate($date) {
    $time = strtotime($date);
    return date("Y-m-d H:i:s", $time);
}

echo convertDate($date1)."\n";
echo convertDate($date2)."\n";

This will print

2019-05-26 22:01:00

2019-03-11 10:58:33

Andrea Olivato
  • 2,450
  • 1
  • 18
  • 30
  • No problem, glad I could help. If this solved your question please remember to mark it as the accepted solution, in order to help future users with similar problems. Have a great day – Andrea Olivato May 31 '21 at 08:10
  • My answer is the same of @AndreaOlivato with the only difference that I used a class and he is using functions. I think both works fine. – sensorario May 31 '21 at 08:47
0

Php provide DateTime class that make the work for you. For example with the imput 2019-05-26T22:01:00.000Z you can do this:

php > $data = new DateTime('2019-05-26T22:01:00.000Z');
php > echo $data->format('Y-m-d H:i:s');
2019-05-26 22:01:00

Again, ... with the input 2019-03-11 10:58:33.979+00 you can do this:

php > $data = new DateTime('2019-03-11 10:58:33.979+00');
php > echo $data->format('Y-m-d H:i:s');
2019-03-11 10:58:33

No matter the input format is. DateTime recognize it and treat or manage it for you.

Your function can be converted in this way:

protected function convertDate($date)
{
    $data = new DateTime($date);
    return $data->format('Y-m-d H:i:s');
}

A lesser readable way to write the same code is:

protected function convertDate($date)
{
    return (new DateTime($date))->format('Y-m-d H:i:s');
}
sensorario
  • 20,262
  • 30
  • 97
  • 159