Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DateTime class to string [duplicate]

Currently I am retrieving a date from mssql with the data type of small date time.

The data is : 2013-03-12 00:00:00

I want to store it in a variable and then display on a text box.

And the format I want to display is just 2013-03-12 on the text box.

The message I get is:

catchable fatal error : Object of class DateTime could not be converted to string.

Any idea?

like image 785
Genjo Avatar asked Mar 15 '13 02:03

Genjo


2 Answers

in php you can simply use date_format($date, 'Y-m-d')

<?php
$date = date_create('2013-11-23 05:06:07');
echo date_format($date, 'Y-m-d');
?>

returns 2013-11-23

like image 160
Acuao Avatar answered Nov 15 '22 04:11

Acuao


The following code will give you a date usable for SQL:

$dateTime->format(\DateTime::ISO8601);

-edit-

Actually, I just saw that you want it the other way around. In that case:

$dateTime->format('Y-m-d');
like image 42
DASPRiD Avatar answered Nov 15 '22 03:11

DASPRiD