Jump to content

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--]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.