user9741470
user9741470

Reputation:

Convert date and remove time from a string date php

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?

Upvotes: 3

Views: 8284

Answers (2)

Salvatore
Salvatore

Reputation: 1435

<?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");

?>

Upvotes: 0

Nikita Leshchev
Nikita Leshchev

Reputation: 1844

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');

Upvotes: 5

Related Questions