0

Possible Duplicate:
How to convert DateTime to VarChar

Example

How do you convert a SQL datetime (for example 08/14/2012 1:05:18 PM ) to the mysql way ( that is Y-m-d H:i:s )?

Community
  • 1
  • 1
Average Joe
  • 4,521
  • 9
  • 53
  • 81
  • [strtotime](http://strtotime.onlinephpfunctions.com/) and [date](http://date.onlinephpfunctions.com/) – Green Black Aug 17 '12 at 19:25
  • Considering the vagueness of the question and the tags selected, this does not appear to be a direct duplicate. – Robert Aug 17 '12 at 19:40
  • "How to convert DateTime to VarChar" is not an exact duplicate. it's not even a close duplicate. So that statement goes to the closers! As to the vagueness of the question, I agree with RobB that it IS vague. I did not realize that when I was asking it though. I did not see it as SQL. This question is good cause it drew me nuts for 2 hours. It would help many people who take date & time information from two different fields from within SQL server and to simply insert it into a mysql date field. Not in a million years, I would have search it as DateTime to VarChar conversion. – Average Joe Aug 17 '12 at 23:06

3 Answers3

7

Depends on where you are wanting to convert it.

To convert in your SQL directly:

convert(varchar(23), getdate(), 121)

PHP using the date and strtotime functions:

date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
Robert
  • 8,717
  • 2
  • 27
  • 34
2

Change the format of the date in PHP like so:

<?
$date = date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
echo $date;
?>
raidenace
  • 12,789
  • 1
  • 32
  • 35
2
$var = "08/14/2012 1:05:18 PM";
echo date('Y-m-d H:i:s', strtotime($var));
user1289347
  • 2,397
  • 1
  • 13
  • 16