You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PHP代码获取次日医生预约数据?

修改方案

原代码通过 date('Y-m-d') 获取当日日期来筛选预约数据,要获取次日数据,只需将日期筛选条件替换为次日日期即可,以下是两种常用修改方式:

方式一:使用原生PHP函数strtotime

public function doctorToday(Request $request){
    $doctors = Appointment::with('doctor')
        ->whereDate('date', date('Y-m-d', strtotime('+1 day')))
        ->get();
    return $doctors;
}

方式二:使用Laravel内置Carbon类(更推荐)

Laravel默认集成了Carbon日期处理库,写法更直观易读:

use Carbon\Carbon;

public function doctorToday(Request $request){
    $doctors = Appointment::with('doctor')
        ->whereDate('date', Carbon::tomorrow()->toDateString())
        ->get();
    return $doctors;
}

说明

  • 两种方式核心都是把whereDate的日期参数从当日替换为次日,以此筛选出目标数据。
  • Carbon的tomorrow()方法直接返回次日的日期实例,toDateString()会转换成Y-m-d格式的字符串,语义更清晰,也能避免手动处理时间戳的潜在问题。

内容的提问来源于stack exchange,提问作者Cassey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:45:40