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