how to convet string date to Mysqldate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

how to convet string date to Mysqldate

Post 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;
johniem
Forum Commoner
Posts: 29
Joined: Mon Jul 20, 2009 8:58 am

Re: how to convet string date to Mysqldate

Post 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..
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: how to convet string date to Mysqldate

Post 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;
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

Re: how to convet string date to Mysqldate

Post by varma457 »

@johniem
I would like to put DATE descending order..
IS IT POSSIBLE WITH VARCHAR FORMAT...
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: how to convet string date to Mysqldate

Post by andyhoneycutt »

varma457 wrote:@johniem
I would like to put DATE descending order..
IS IT POSSIBLE WITH VARCHAR FORMAT...
not without converting it.
Post Reply