7

I have to convert this string date 2016-09-26 00:00:00.000 to the yyyy-mm-dd format without other characters.

Can you help me, please?

laurent
  • 88,262
  • 77
  • 290
  • 428
  • Simplest solution is: `$date = preg_replace("/\s{1}\d{2}:\d{2}\:\d{2}/",'',$datetime);` – Grant Aug 20 '21 at 02:51

3 Answers3

10

You can just use the DateTime class along with the format() method:

$d = new DateTime('2016-09-26 00:00:00.000');
echo $d->format('Y-m-d');
laurent
  • 88,262
  • 77
  • 290
  • 428
7

Try this:

$datetime = '2016-09-26 00:00:00.000';

$date = date('Y-m-d', strtotime('2016-09-26 00:00:00.000'));

you will get the only date part in 'yyyy-mm-dd' format.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0
$test = strtotime('2016-09-26 00:00:00.000');
$test1 = date('Y-m-d h:i', $test);
$array1 = explode(' ', $var);
$date_temp = $array1[0];