简体   繁体   English

仅选择月份和日期

[英]Select month and date only

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. 我在这种格式有一个MySQL表(出生日期)的本专栏02-03-2016 ,我想今天的时候诞生的成员的日期显示一条消息。 How can I select only the month and the date 02-03 in the column to compare it with $dat=date("md"); 如何只选择月份和日期02-03的列,将其与$dat=date("md"); . 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>";
 }

This should work although, there are so many elegant solutions. 尽管有很多优雅的解决方案,但这应该可以工作。 Also, as suggested by others verify whether the db stores date in dmY or mdY format 另外,按照其他人的建议,验证数据库是否以dmY或mdY格式存储日期。

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. 该脚本检查DateOfBirth字段中的前5个字符是否等于您在$ dat中创建的字符串。

A more elegant way would be do use the inbuilt date formatting, ie 一种更优雅的方法是使用内置的日期格式,即

 DATE_FORMAT(DateOfBirth,'%m-%d') = $dat
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. 您可以使用DATE_FORMAT临时转换日期格式,并检查当前日期和月份。 here goes your tutorial on DATE_FORMAT http://www.w3schools.com/sql/func_date_format.asp 这是您在DATE_FORMAT上的教程http://www.w3schools.com/sql/func_date_format.asp

There are some function are available in mysql to format your date so you can use those mysql中有一些功能可以格式化您的日期,因此您可以使用这些功能

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()将字符串转换为MySQL日期值,或使用DATE_FORMAT函数将日期转换为所需的任何格式

 STR_TO_DATE(datestring, '%d/%m/%Y')
DATE_FORMAT(date,'%d/%m/%Y')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM