select('id, birthday, birthdayTime, birthdayMonth, birthdayDate, lunar') ->where(['lunar' => 1]) ->andWhere(['<', 'birthdayTime', $now]) ->orderBy('id ASC'); foreach ($query->batch(200) as $userList) { foreach ($userList as $user) { $userId = $user->id; $birthdayMonth = $user->birthdayMonth; $birthdayDate = $user->birthdayDate; $year = (int)date('Y'); try { // 农历转公历,有多处使用,需要同步修改,关键词 change_my_birthday $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate); $month = $result['gregorian_month'] ?? 1; $day = $result['gregorian_day'] ?? 1; $birthday = $year . '-' . $month . '-' . $day; // 今年阳历生日已过,则换算明年农历对应阳历 if (strtotime($birthday) < $todayStart) { $year++; $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate); $month = $result['gregorian_month'] ?? 1; $day = $result['gregorian_day'] ?? 1; $birthday = $year . '-' . $month . '-' . $day; } } catch (\InvalidArgumentException $e) { noticeUtil::push( '阴历转阳历脚本单条失败,userId:' . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $e->getMessage(), '15280215347' ); continue; } $birthdayTime = strtotime($birthday); if ($birthdayTime <= 0) { continue; } // 先更新用户表(生日源数据),再按 userId 同步门店客户 UserClass::updateById($userId, [ 'birthday' => $birthday, 'birthdayTime' => $birthdayTime, ]); CustomClass::updateByCondition(['userId' => $userId], [ 'birthday' => $birthday, 'birthdayTime' => $birthdayTime, ]); $updatedUserCount++; } } Yii::info('阴历转阳历脚本成功执行,更新用户数:' . $updatedUserCount); echo "阴历转阳历完成,更新用户数:{$updatedUserCount}\n"; } catch (\Exception $e) { Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage()); echo '阴历转阳历脚本执行失败,原因:' . $e->getMessage() . "\n"; } } }