dpwtr666638 2016-06-28 18:10
浏览 51

PHP数据库DATE与今天的DATE相比

In the database, I Have DOB table wich is DATE . so I have problem in search function, because I want to search people by Date to be correct age at any time. So fields in search is AGE for example 18-25 , . I want to make searchage1 = 18 in this example and searchage2 = 25.

 `dob BETWEEN ".$search_data['searchage1']." AND ".$search_data['searchage2'];`

I want to convert this 18 and 25 into DATE then Compare it to Database DATES . I have tried some possiblity with no sucess, any idea?

  • 写回答

1条回答 默认 最新

  • douyao1856 2016-06-28 18:15
    关注

    Firstly, you need to calculate the age based form the dob field, then use this in your WHERE statements.

    Please see these threads on how to calculate age in SQL: here or here

    EG:

    SELECT *, DATEDIFF(dob, '2016-06-28')/365.25 AS `age` WHERE `age` >= '18' AND `age` =< '25';
    

    or

    SELECT *, YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)) AS `age` WHERE `age` >= '18' AND `age` =< '25';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)