0

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)
3
  • 4
    The basic problem appears to be that you're mixing PHP and SQL without understand which is which and what engine is executing what code. Commented May 28, 2012 at 19:49
  • 1
    You can't use php functions in mysql, and you can't use mysql functions in PHP. They're two completely separate systems and languages that happen to be able to talk to each other. Commented May 28, 2012 at 19:51
  • Yes, I admit, I sometimes get mysql and php commands confused, all my scripts are being written in php. I am still new to both languages. Commented May 28, 2012 at 20:10

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

ahhh, hits head I did not think to echo it. I also did not realize that the date function was able to convert it for me. This did it for me! Thank you!
Also, thank you for editing my question, I cant seem to get a hang of stack overflow's syntax highlighting, and if i was posting more then a couple lines, ending up throwing the code into pastebin and linking it
@CampSoup1988: There is guideline to the right side of the textbox on how to format it. You can press Ctrl+K for code.
True, but when I am copying a good portion of my site, it seemed to break up my code unnaturally and i think removing parts of it. I dont recall off hand.
0

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

3 Comments

I was using html5 input type=date to store the data into the database
how does that make difference
I did not convert the value of the input before storing it in the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.