-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
  • 527
  • 4
  • 20
chgav007
  • 85
  • 1
  • 13

2 Answers2

7

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,689
  • 1
  • 13
  • 24
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,790
  • 6
  • 21
  • 37