Skip to content

Birth Date

The BirthDate value object is used to properly define the birth date of customers, users, etc..

Its usage is quite simple and there's only a few parameters to be passed through the constructor, however, there's a few handy methods where you can convert Carbon objects or dates that are simple just strings into BirthDate objects 😏

Parameters

year integer  required

The birth date year.


month integer  required

The birth date month.


day integer  required

The birth date day.

Usage

Using the __construct()

php
use Neue\Framework\Foundation\ValueObjects\BirthDate;

$birthDate = new BirthDate(
    year: 2022,
    month: 1,
    day: 1,
);

Using the fromCarbon method

This method allows an instance of Carbon to be provided and it will automatically convert it to a BirthDate instance.

php
use Carbon\Carbon;
use Neue\Framework\Foundation\ValueObjects\BirthDate;

$carbon = Carbon::parse('2022-1-1');

$birthDate = BirthDate::fromCarbon($carbon);

Using the fromString method

This method allows a date as a string to be provided which will then be automatically parsed by Carbon and converted to a BirthDate instance.

php
use Neue\Framework\Foundation\ValueObjects\BirthDate;

$birthDate = BirthDate::fromString('2022-1-1');

WARNING

An exception will be thrown if Carbon is not able to parse the given date.

Output

Using any of the examples above, we can perform the following operations to retrieve different information from the BirthDate object.

php
(string) $birthDate; // 2022-1-1

$birthDate->date->format('d/m/Y'); // 1/1/2022
Birth Date has loaded