Jump to content

date_format() in MySQL statment


rastlin

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/3651-date_format-in-mysql-statment/
Share on other sites

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

[code]$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'";[/code]


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] * [color=green]FROM[/color] [color=orange]table_name[/color] [color=green]WHERE[/color] date_format(db_date,[color=red]'%m %d'[/color]) [color=orange]=[/color] date_format([color=red]'$date'[/color], [color=red]'$m $d'[/color]) [!--sql2--][/div][!--sql3--]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.