Laravel: How to get the Year from Date and Timestamp


Last Updated on July 20, 2023

If you need to get the year from a date or timestamp in Laravel then you will need the Carbon extension for DateTime in PHP.

Below are 4 examples of how you can get the year from date and timestamps.

  1. Getting the year from Date
  2. Getting the year from Database Table’s Date column
  3. Getting the year from Timestamp
  4. Getting the year from Database Table’s Timestamp column

Example #1: Get the year from date

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function index() {
        // Date in string format
        $eventDate = '2023-07-19';

        // Convert String to Carbon Date
        $eventDate = new Carbon($eventDate);

        // Get the year from Carbon date
        // Data type: integer
        $eventYear = $eventDate->year;

        dd($eventYear); // Output: 2023
    }
}


Example #2: Get the year from database table’s date column

<?php

namespace App\Http\Controllers;

use App\Models\Event;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function index() {

        // Get row from database
        $event = Event::find(1);

        // Get the event_date column
        // Data Type: String
        $eventDate = $event->event_date;    

        // Convert to Carbon Date
        $eventDate = new Carbon($eventDate); 

        // Get the year from Carbon date
        // Data type: integer
        $eventYear = $eventDate->year;

        dd($eventYear);
    }
}


Example #3: Get the year from timestamp

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function index() {
        
        // timestamp in string format
        $timestamp = '2023-07-19 07:54:20';

        // Convert String to Carbon Date
        $timestamp = new Carbon($timestamp);

        // Get the year from Carbon date
        // Data type: integer
        $timestampYear = $timestamp->year;

        dd($timestampYear); // Output: 2023
    }
}


Example #4: Get the year from database table’s timestamp column

<?php

namespace App\Http\Controllers;

use App\Models\Event;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function index() {

        // Get row from database
        $event = Event::find(1);

        // Get the created_at timestamp column
        // Type: object
        // Object Class: Illuminate\Support\Carbon
        $createdAt = $event->created_at;    

        // Convert to Carbon Date
        $createdAt = new Carbon($createdAt); 

        // Get the year from Carbon date
        // Data type: integer
        $createdAtYear = $createdAt->year;

        dd($createdAtYear);
    }
}

The line $createdAtYear = $createdAt->year; is optional since the return of the created_at and the updated_at columns is already a Carbon DateTime.


I hope the above examples help you with getting the year from date and timestamp using Laravel.

Let me know your experience in the comments below.

,

Leave a Reply

Your email address will not be published. Required fields are marked *