0

i am storing date and time in database using php using gmdate() function in format "Y-m-d H:i:s". for e.g.

2014-03-10 12:35:55

Now,on getting this data into a php variable,for e.g.

$temp=2014-03-10 12:35:55

can i extract and display only the DATE portion excluding the TIME portion?? I am new to date and time manipulation.Any help?

echo_Me
  • 37,078
  • 5
  • 58
  • 78
Ankur
  • 269
  • 2
  • 4
  • 15

5 Answers5

4

you can get directly the date from database like this

select DATE(column_datetime) as date from yourtable
echo_Me
  • 37,078
  • 5
  • 58
  • 78
0
date('Y-m-d', strtotime($temp));  

The above statement returns the date from datetime format

user3004356
  • 870
  • 4
  • 16
  • 49
0

As Pramod answered,

date('Y-m-d', strtotime($temp)); 

works.

user3004356
  • 870
  • 4
  • 16
  • 49
Ankur
  • 269
  • 2
  • 4
  • 15
  • A little note to that http://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php#115864 – secelite Mar 19 '14 at 11:42
  • its not safe to use strtotime but strptime is not available for windows.whats the solution now? – Ankur Mar 19 '14 at 11:49
0

The following code example is adapted by this thread: How to convert date to timestamp in PHP?

There you can also read why it is not safe to rely on strtotime (answer by daremon)

$temp = '2014-03-10 12:35:55';
$a = strptime($temp, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);

Edit

From the PHP manual

Note: This function is not implemented on Windows platforms.

Community
  • 1
  • 1
secelite
  • 1,353
  • 1
  • 11
  • 19
-1

Try this:

Echo date('Y-M-D', $var);

K.W.
  • 141
  • 1
  • 15