Jump to content


Photo

date_format() in MySQL statment


  • Please log in to reply
1 reply to this topic

#1 rastlin

rastlin
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 February 2006 - 02:22 AM

This is likely simple but I was wondering what the display of date_format() look like when/if it used like this.

date_format(db_date,'%m %d') = '$monthDay'

say if date in the DB was 2006-02-26 would the date_format() above look like 02-26? So if $monthDay equaled 02-26 they would match....

SQL query looks like this
$query = "SELECT * FROM table_name WHERE date_format(db_date,'%m %d') = '$monthDay'";


Thanks for your help!
Stephen

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 27 February 2006 - 02:32 AM

date_format is not a php function, therefor you would have to use php's strtotime and date functions for format your dates correctly.

$date = '2006-02-26';
$date = strtotime($date);

$newdate = date("m-d", $date);

$query = "SELECT * FROM table_name WHERE date_format(db_date,'%m %d') = '$newdate'";


Or, this might work, but don't hold me to it:

$date = '2006-02-26';

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM table_name WHERE date_format(db_date,'%m %d') = date_format('$date', '$m $d') [!--sql2--][/div][!--sql3--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users