0

Currently I have a datetime like this:

2012-07-24 11:52:01

Now i want to show only date like 2012-07-24.

Can anyone give any php function? Thanks.

Vhndaree
  • 594
  • 1
  • 6
  • 20
tanvirjahan
  • 164
  • 1
  • 1
  • 8

4 Answers4

8

just follow the following procedure

date("Y-m-d",strtotime("2012-07-24 11:52:01"));

in above code strtotime() returns value of date in seconds and "Y-m-d" transfers it in Y-M-D format of date as date() method is used.

Vhndaree
  • 594
  • 1
  • 6
  • 20
6

Go ahead and use PHP's DateTime class. I was reluctant to move away from functions to classes, but it's really not that hard. Just pass the date string into the class object and then use the format method to output the date string in the way you want.

http://php.net/manual/en/class.datetime.php

$dt = new DateTime('2012-07-24 11:52:01');
echo $dt->format('Y-m-d');
jjwdesign
  • 3,272
  • 8
  • 41
  • 66
5
$date = "2012-07-24 11:52:01";
$pieces = explode(" ", $date);
echo $pieces[0];
Community
  • 1
  • 1
Ameo
  • 2,307
  • 5
  • 21
  • 33
  • 2
    While this code may answer the question, providing additional context regarding _why_ and/or _how_ this code answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Mar 16 '16 at 18:24
0

Check php manual for more reference: http://php.net/manual/en/function.date.php

Little research please

$today = date("Y-m-d ");

TacoTuesday
  • 67
  • 1
  • 11