如何设置日期,相隔n天

时间:2018-10-30 10:57:46

标签: laravel eloquent

我目前有将所有当前提醒(4)设置为同一日期的代码。我该如何分开设置这87天。

foreach ($request->pets as $pet) {
  $clientPet = $client->pets()->find($pet)->first();
  $clientPet->name = $pet['name'];
  $clientPet->treated_at = $pet['treatedAt'];

  $clientPet->save();

  foreach($clientPet->reminders as $reminders)
  {

    $reminders->scheduled_at   = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at)->addWeeks(12)->subDays(3);
    $reminders->product_id     = $pet['productId'];
    $reminders->application_id = $pet['applicationId'];

    $reminders->save();
  }

}

2 个答案:

答案 0 :(得分:0)

您没有在代码中引用最后一个日期,但是您总是在原始日期前添加87天(或很多天),因此请尝试

$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminder)
{

   $reminder->scheduled_at   = $date->addDays(87);
   $reminder->product_id     = $pet['productId'];
   $reminder->application_id = $pet['applicationId'];

   $reminder->save();
}

由于Carbon是易变的,因此每个$reminder都会比前一天多获得87天。

答案 1 :(得分:0)

使用此

foreach ($request->pets as $pet) {
   $clientPet = $client->pets()->find($pet)->first();
   $clientPet->name = $pet['name'];
   $clientPet->treated_at = $pet['treatedAt'];

   $clientPet->save();

   foreach($clientPet->reminders as $reminders)
   {

     $reminders->scheduled_at   = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at)->addDays(10);
     $reminders->product_id     = $pet['productId'];
     $reminders->application_id = $pet['applicationId'];

     $reminders->save();
   }

}