嵌套函数; .todays(datediff(student_info,' 1999-8-31'))

时间:2016-11-15 12:25:51

标签: php

我是sql和php的新手。我想通过dob(表格字段)和固定日期(;。31-8-2016)的差异来计算学生的年龄(以月为单位)。并且还希望将该值存储在名为' age'

的字段中

1 个答案:

答案 0 :(得分:0)

  

使用MYSQL

SELECT 12 * (YEAR(DateOfService) 
              - YEAR(BirthDate)) 
       + (MONTH(DateOfService) 
           - MONTH(BirthDate)) AS months 
FROM table
  

使用PHP

$date1 = '2000-01-25';
$date2 = '2010-02-20';

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);