CampSoup1988
CampSoup1988

Reputation: 139

Convert DATE to DATETIME in php and mySQL

In my mySQL database I have a field called FlowerOpen that is in the DATE format (YYYY-MM-DD) in my php script, I want to convert it so the user sees Monday, May 28, 2012

I keep finding explanations on how to convert DATETIME to DATE but not DATE to DATETIME

I tried converting the date with the following command:

$Date = DATE_FORMAT($ResultsLists['FlowerOpen'],'%W, %M %e, %Y');

I keep getting the following error:

Warning: date_format() expects parameter 1 to be DateTime, string given in
C:\xampp\htdocs\Orchid\viewPlantInfo.php on line 383

So I tried converting the FlowerOpen to DATETIME and then format it using:

$Date = date("Y-m-d H:i:s", strtotime($ResultsLists['FlowerOpen']));
$Date = DATE_FORMAT($Date,'%W, %M %e, %Y');

but I got the same error message.

I also tried

$Date = SELECT CAST($ResultsLists['FlowerOpen'] AS DATETIME);
$Date = DATE_FORMAT($Date,'%W, %M %e, %Y');

but I got the error

Parse error: syntax error, unexpected T_STRING in 
C:\xampp\htdocs\Orchid\viewPlantInfo.php on line 382 (the CAST line)

Upvotes: 0

Views: 9584

Answers (2)

sohaan
sohaan

Reputation: 261

you can store your DATETIME in mysql by converting them to unix timestamp format using mktime() function the when you retrive it back from database you can use date() function to convert it to your required format .

P.S converting datatime to unix timestamp before storing it in database can give you numerous advatages later when you want to work with dates

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382909

You can convert it via php using strtotime like this:

echo date('l, M d, Y', strtotime($yourDate));

Working Example

echo date('l, M d, Y', strtotime('2012-05-29')); // Tuesday, May 29, 2012

Upvotes: 1

Related Questions