|
63 | 63 | - [Extract Text Between Strings](#laravel-tip--extract-text-between-strings-️)
|
64 | 64 | - [Reusable Pipelines](#laravel-tip--reusable-pipelines-️)
|
65 | 65 | - [Clamp Numbers](#laravel-tip--clamp-numbers-️)
|
| 66 | +- [Date Checks with Carbon](#laravel-tip--date-checks-with-carbon-️) |
66 | 67 |
|
67 | 68 | ## Laravel Tip 💡: The "squish" method ([⬆️](#helpers-tips-cd-))
|
68 | 69 |
|
@@ -1183,3 +1184,36 @@ Product::all()->select('name', 'price');
|
1183 | 1184 | * - only() can't handle nested arrays
|
1184 | 1185 | */
|
1185 | 1186 | ```
|
| 1187 | + |
| 1188 | +## Laravel Tip 💡: Date Checks with Carbon ([⬆️](#helpers-tips-cd-)) |
| 1189 | + |
| 1190 | +I know you've had to check if a date has expired or is in the future at least once. Since Laravel uses Carbon under the hood, you've got access to a ton of helpers to handle all that elegantly 🚀 |
| 1191 | + |
| 1192 | +```php |
| 1193 | +<?php |
| 1194 | + |
| 1195 | +use Illuminate\Support\Carbon; |
| 1196 | + |
| 1197 | +$date = Carbon::createFromDate(2025, 7, 28); |
| 1198 | + |
| 1199 | +$date->isToday(); // true |
| 1200 | +$date->isTomorrow(); // false |
| 1201 | +$date->isYesterday(); // false |
| 1202 | + |
| 1203 | +$date->isWeekday(); // true |
| 1204 | +$date->isWeekend(); // false |
| 1205 | + |
| 1206 | +$date->isFuture(); // false |
| 1207 | +$date->isPast(); // false |
| 1208 | + |
| 1209 | +// But wait, there's more 🔥 |
| 1210 | +$date->isSameDay(now()); // true |
| 1211 | +$date->isSameWeek(now()); // true |
| 1212 | +$date->isSameMonth(now()); // true |
| 1213 | +$date->isSameYear(now()); // true |
| 1214 | + |
| 1215 | +// You can even specify a unit |
| 1216 | +$date->isSameUnit('year', now()); // true |
| 1217 | + |
| 1218 | +// And the list goes on... 😎 |
| 1219 | +``` |
0 commit comments