1. 輸入日期查詢年齡?

??2. php laravel框架實現 代碼
/*** 在線年齡計算器*/public function ageDateCal(){// 輸入的生日時間$birthday = $this->request('birthday');// 當前時間$currentDate = date('Y-m-d');// 計算周歲$age = date_diff(date_create($birthday), date_create($currentDate))->y;// 計算虛歲$nominalAge = $age + 1;// 計算已生活天數$days = date_diff(date_create($birthday), date_create($currentDate))->days;// 計算已生活分鐘數// 獲取當前時間戳$currentTimestamp = time();// 轉換日期字符串為時間戳$inputTimestamp = strtotime($birthday);// 計算時間差(以分鐘為單位)$minutes = round(abs($currentTimestamp - $inputTimestamp) / 60);// 計算距離一百歲還有多少年多少月多少日合計多少天$hundredYears = date("Y-m-d H:i:s", strtotime("$birthday+100year"));$diffDate = $this->diffDate($currentDate, $hundredYears);$remainingYears = $diffDate['y'];$remainingMonths = $diffDate['m'];$remainingDays = $diffDate['d'];$remainingTotalDays = $diffDate['a'];// 計算下次生日的日期和天數$nextBirthday = date('Y-m-d', strtotime($birthday . ' + ' . ($age + 1) . ' years'));$daysToNextBirthday = date_diff(date_create($currentDate), date_create($nextBirthday))->days;$week = $this->getWeekDay($nextBirthday);$data = ['age' => $age,'nominalAge' => $nominalAge,'days' => $days,'minutes' => $minutes,'remainingYears' => $remainingYears,'remainingMonths' => $remainingMonths,'remainingDays' => $remainingDays,'remainingTotalDays' => $remainingTotalDays,'nextBirthday' => $nextBirthday,'week' => $week,'daysToNextBirthday' => $daysToNextBirthday,];return $this->jsonSuc(['result' => $data]);}/*** 判斷星期幾* @param $time* @return string*/public function getWeekDay($time){$week_array = ['日', '一', '二', '三', '四', '五', '六'];$week = date("w", strtotime($time));return '星期' . $week_array[$week];}
/*** function:計算兩個日期相隔多少年,多少月,多少天* param string $date1[格式如:2020-11-5]* param string $date2[格式如:2023-12-01]* return array array('年','月','日');*/function diffDate($date1, $date2){$datetime1 = new \DateTime($date1);$datetime2 = new \DateTime($date2);$interval = $datetime1->diff($datetime2);$time['y'] = $interval->format('%Y');$time['m'] = $interval->format('%m');$time['d'] = $interval->format('%d');$time['h'] = $interval->format('%H');$time['i'] = $interval->format('%i');$time['s'] = $interval->format('%s');$time['a'] = $interval->format('%a'); // 兩個時間相差總天數return $time;}