今天工作要计算用户的生日到周,找到一个函数。
- /**
- * 计算生日到现在的时间
- * @param int $birthday 宝贝生日
- */
- function age($birthday) {
- $now = date('Ymd');
- //分解当前日期为年月日
- $y = (int) ($now / 10000);
- $m = (int) (($now % 10000) / 100);
- $d = $now % 100;
- //分解生日为年月日
- $yy = (int) ($birthday / 10000);
- $ym = (int) (($birthday % 10000) / 100);
- $yd = $birthday % 100;
- //特殊日期计算
- if ($years % 4 == 0 || ($years % 100 == 0 && $years % 400 == 0)) {
- $special = 29;
- } else {
- $special = 28;
- }
- //各月的上个月的天数
- switch ($m) {
- case 2:$add = 31;
- case 3:$add = 31;
- case 6:$add = 31;
- case 8:$add = 31;
- case 9:$add = 31;
- case 11:$add = 31;
- case 1:$add = 31;
- case 5:$add = 30;
- case 7:$add = 30;
- case 10:$add = 30;
- case 12:$add = 30;
- case 3:$add = $special;
- }
- if ($d < $yd) {
- $d+=$add;
- $m-=1;
- }
- if ($m < $ym) {
- $m+=12;
- $y-=1;
- }
- //运算年龄
- if ($now >= $birthday) {
- $age['day'] = $d - $yd;
- $age['month'] = $m - $ym;
- $age['year'] = $y - $yy;
- return $age;
- }else{
- return 0;
- }
- }