There is already a date in the format

2012-05-05 00:00:00 

How to reduce, leaving only the date, and the hours, minutes and seconds to remove?

Of course, you can explode , but there may be something more efficiently available, for example. through the same date ?

    4 answers 4

    At least three options can be called immediately:

     // 1 $date_str = '2012-05-05 00:00:00'; echo substr($date_str, 0, strpos($date_str, ' ')); // 2 $date = new DateTime($date_str); echo $date->Format('Ym-d'); // 3 echo date_create($date_str)->Format('Ym-d'); 

    I do not like the first, and you choose.

    • one
      Yes, the 3rd came, thanks! - Smash
    • beautiful :) - xEdelweiss
    • 2
      strstr($date_str, ' ', true); :) - E_p
    • @E_p thanks. We should remember her. - xEdelweiss
     $date = strtotime('2012-05-05 00:00:00'); Date('m/d/Y',$date) 
    • the date is already available , I do not need the current date. - Smash
    • Well, I thought it was simple. corrected. - Vahan Av
     <?php $date = date_create('2000-01-01'); echo date_format($date, 'Ymd H:i:s'); ?> 

      If you want to output a date without zeros, then date('Yn-j')