laravel carbon get year number 1

laravel carbon get year number

use Carbon\Carbon;
// Date = 7th of July 2021
$year = Carbon::now()->format('Y'); // 2021
$year = Carbon::now()->format('y'); // 21
$year = Carbon::now()->year; // 2021

Here is what the above code is Doing:
1. We’re creating a new instance of the Carbon class.
2. We’re using the now() method to get the current date and time.
3. We’re using the format() method to format the date and time.
4. We’re passing the format() method the ‘Y’ parameter to get the year in 4 digits.
5. We’re passing the format() method the ‘y’ parameter to get the year in 2 digits.
6. We’re using the year property to get the year in 4 digits.

Similar Posts