3

I have this two DateTime values:2018-08-19T16:00:00Z and 1534694400. For the first value i need to remove the T16:00:00Z from the date, for the second value, I need to convert it in a normal date. How I can do this using the DateTime class of php?

  • Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – user3942918 Aug 20 '18 at 05:48

2 Answers2

5

For the first date:

$dt = new \DateTime('2018-08-19T16:00:00Z');
echo $dt->format('Y-m-d');

For timestamp:

$dt = new \DateTime();
$dt->setTimestamp(1534694400);
echo $dt->format('Y-m-d');
Nikita Leshchev
  • 1,784
  • 2
  • 14
  • 26
  • Thanks for the help, I was reading the `PHP` manual about this class but I was not sure on how to proceed. I've just one question about the values to convert, can I pass that values without problems if I extract them from an array? –  Aug 19 '18 at 14:24
  • 1
    @user9741470 Sure. If there're same values in array, you can pass them into DateTime's constructor without any worries. E.g. if `$array[0] = '2018-08-19T16:00:00Z'` then you can pass this value into constructor: `new \DateTime($array[0]);` – Nikita Leshchev Aug 19 '18 at 14:46
  • 1
    Ok, thank you for the help. I was testing the solution you've suggested and it works smoothly. –  Aug 19 '18 at 14:52
0
<?php

  //1st
  $date=date_create("2018-08-19T16:00:00Z");
  echo date_format($date,"Y-m-d");

  //2nd
  $date=date_create();
  date_timestamp_set($date,1534694400);
  echo date_format($date,"Y-m-d");

?>
Salvatore
  • 1,435
  • 1
  • 10
  • 21