从varchar存储日期格式获取朋友生日的日期

时间:2012-08-21 11:56:02

标签: mysql

我想从varchar存储日期(例如12-21-1990)获取日期。我可以使用php explode来获取当前日期,但我想通过mysql获取它。我正在使用这样的查询:

$count=mysql_query("SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id "); 

以上查询向我的用户朋友提供了所有详细信息,但我只想获得今天生日那天的朋友deatails。我怎么能这样做?

6 个答案:

答案 0 :(得分:2)

你可以通过php date函数获取今天的日期,将其分配给变量并检查u.bday。 像这样:

$count=$today = date("m-d-Y");

mysql_query("SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id AND u.bday = $today");

答案 1 :(得分:0)

我会将bday列数据类型更改为DATE并使用CUREDATE()函数

答案 2 :(得分:0)

您只需使用STR_TO_DATE MySQL函数将varchar转换为日期:

$count=mysql_query("SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id AND STR_TO_DATE(u.bday, '%m-%d-%Y')=DATE_FORMAT(CURDATE().'%m-%d-%Y') "); 

但我强烈建议您将bday列更改为日期类型。

答案 3 :(得分:0)

试试这个:

date_format(your_friend_birthdate,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')

完整查询: -

    SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id 
and month(u.bday)=month(now()) and date(u.bday)=date(now());

SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
    friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id 
    and month(u.bday)=month(now()) and day(u.bday)=day(now());

答案 4 :(得分:0)

SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id 
and CAST(CONCAT(SUBSTRING(u.bday,7,11),'/',SUBSTRING( u.bday,1,2 ),'/',SUBSTRING(u.bday,4,2 )) AS DATE) = date_format(now(),'%Y-%m-%d');

答案 5 :(得分:0)

你好刚刚找到了解决方案并希望分享它,以便它可以帮助某人。

SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
    friend_list f, users_profile u WHERE f.uid = '3' AND f.status = '1' AND u.uid = f.friend_id 
      and    DAY(STR_TO_DATE(u.bday, '%m-%d-%y')) = DAY(CURDATE()) AND
    MONTH(STR_TO_DATE(u.bday, '%m-%d-%y')) = MONTH(CURDATE())