Reputation: 73
I have this column of a mysql table (DateOfBirth) in this format 02-03-2016
I would like to display a message when member's date of birth is today. How can I select only the month and the date 02-03
in the column to compare it with $dat=date("m-d");
. Behold my codes
date_default_timezone_set("America/New_York");
$dat=date("m-d");
$resa=mysqli_query($dbc,"SELECT name FROM qbreplace.Staff_Member WHERE
DateOfBirth = '$dat'") or die ('Erreur : '.mysqli_error());
while($row = mysqli_fetch_array($resa)){
echo "<div style='color:red;font-weight:bold;text-decoration:blink;'>Today is ".$row['name']." birthday !</div>";
}
Upvotes: 0
Views: 122
Reputation: 609
This should work although, there are so many elegant solutions. Also, as suggested by others verify whether the db stores date in d-m-Y or m-d-Y format
date_default_timezone_set("America/New_York");
$dat=date("m-d");
$resa=mysqli_query($dbc,"SELECT name FROM qbreplace.Staff_Member WHERE
LEFT(DateOfBirth , 5) = '$dat'") or die ('Erreur : '.mysqli_error());
while($row = mysqli_fetch_array($resa)){
echo "<div style='color:red;font-weight:bold;text-decoration:blink;'>Today is ".$row['name']." birthday !</div>";
}
The script checks for whether the first 5 characters in the DateOfBirth field is equal to the string you create in $dat.
A more elegant way would be do use the inbuilt date formatting, ie
DATE_FORMAT(DateOfBirth,'%m-%d') = $dat
Upvotes: 1
Reputation: 2683
date_default_timezone_set("America/New_York");
$dat=date("m-d");
$resa=mysqli_query($dbc,"SELECT name FROM qbreplace.Staff_Member WHERE
DATE_FORMAT(DateOfBirth,'%m-%d') = '$dat'") or die ('Erreur : '.mysqli_error());
while($row = mysqli_fetch_array($resa)){
echo "<div style='color:red;font-weight:bold;text-decoration:blink;'>Today is ".$row['name']." birthday !</div>";
}
you can use DATE_FORMAT
to convert your date format temporarily and check the present date and month.
here goes your tutorial on DATE_FORMAT
http://www.w3schools.com/sql/func_date_format.asp
Upvotes: 1
Reputation: 255
There are some function are available in mysql to format your date so you can use those
You can use STR_TO_DATE() to convert your strings to MySQL date values or DATE_FORMAT function to convert your date into any format which you required
STR_TO_DATE(datestring, '%d/%m/%Y')
DATE_FORMAT(date,'%d/%m/%Y')
Upvotes: 0