-1

how can I get and store date('2017-02-02') in php variable from result : datetime("2017-02-02 17:02:03").

Bharat Dangar
  • 557
  • 3
  • 18
chgav007
  • 83
  • 1
  • 12

2 Answers2

5

Do you mean getting 2017-02-02 from 2017-02-02 17:02:03, well you have many options,

$date = date('Y-m-d', strtotime('2017-02-02 17:02:03'));

OR

list($date) = explode(" ", '2017-02-02 17:02:03');

OR

    $arr = explode(" ", '2017-02-02 17:02:03');
    $date = array_shift($arr);

OR

 $dateObj = DateTime::createFromFormat("Y-m-d H:i:s", '2017-02-02 17:02:03');
 $date = $dateObj->format("Y-m-d");
beta-developper
  • 1,707
  • 1
  • 13
  • 21
1

You need to convert the date to a timestamp with strtotime()

Example:

<?php
echo date("Y-m-d", strtotime('2017-02-02 17:02:03'));
?>

I hope this will help you!

node_modules
  • 4,122
  • 4
  • 18
  • 34