[英]Issue with Laravel Carbon next() day
我有這個代碼:
return Carbon::createFromFormat('Y-m-d', $given_date, $user_timezone)->next($user_day); // $user_day = 'Monday';
這給出了錯誤:
未定義指數:星期一
任何幫助表示贊賞。
通過查看 function 本身:
// Carbon\CarbonInterface
/**
* Modify to the next occurrence of a given modifier such as a day of
* the week. If no modifier is provided, modify to the next occurrence
* of the current day of the week. Use the supplied constants
* to indicate the desired dayOfWeek, ex. static::MONDAY.
*
* @param string|int|null $modifier
*
* @return static
*/
public function next($modifier = null);
您可以得出您需要使用碳常數,即Carbon::MONDAY
。 查看 class 本身,解析為1
。
因此,您也可以從0
(星期日)到6
(星期六)傳遞任何 integer。
共享 $given_date 和 $user_timezone 的值,這將有助於調試。 另外 Carbon::createFromFormat() 生成一個碳 object,想知道為什么它會拋出未定義的索引,做一個 dd(Carbon::createFromFormat('Ym-d', $given_date, $user_timezone)); 看看會發生什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.