Page 1 of 1

how to convet string date to Mysqldate

Posted: Tue Jul 21, 2009 8:33 am
by varma457
Hi,

I have a problem with the date.
I am getting date as string from file.I would like to convert this string to Mysql date.I dont want to insert date as a varchar.

I did like ,,it is not working..

$phpdate1='10/02/2000';
$mysqldate=date('y-m-d',$phpdate1);
echo $mysqldate;

Re: how to convet string date to Mysqldate

Posted: Tue Jul 21, 2009 9:18 am
by johniem
What exactly you want to do with the date field??
Why you don't want to use VARCHAR??

You can use STR_TO_DATE(date,dateformat) function in mysql to convert a string into date..

Re: how to convet string date to Mysqldate

Posted: Tue Jul 21, 2009 11:33 am
by andyhoneycutt
I prefer dates/datetimes over strings as dates any day. One reason is conditional logic. Is "01/01/2009" > "10/01/2000" ? Not if they're strings, it's not. So, rather than adding a layer of conversion on top of your queries to compare dates, if you can, store them as dates instead of strings.

Plus, your usage here is wrong, varma457:

Code: Select all

$phpdate1='10/02/2000';
$mysqldate=date('y-m-d',$phpdate1);
echo $mysqldate;
The date() function accepts a second parameter as a time (int) value. Try this instead:

Code: Select all

$phpdate1='10/02/2000';
$mysqldate = date('Y-m-d',strtotime($phpdate1));
echo $mysqldate;

Re: how to convet string date to Mysqldate

Posted: Tue Jul 21, 2009 1:38 pm
by varma457
@johniem
I would like to put DATE descending order..
IS IT POSSIBLE WITH VARCHAR FORMAT...

Re: how to convet string date to Mysqldate

Posted: Wed Jul 22, 2009 5:07 pm
by andyhoneycutt
varma457 wrote:@johniem
I would like to put DATE descending order..
IS IT POSSIBLE WITH VARCHAR FORMAT...
not without converting it.