You can retrieve the date of birth (dob) value by the customer id or email in Magento 2. Birthdate value needs to disable by default so you can’t able to see on the register page.
You can enable it from the Configuration setting to display the Dob field in the Customer Registration page.
Dob returns the date value as Y-m-d(1990-10-10) format.
<?php namespace Jesadiya\Dob\Model; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; class Dob { /** * @var CustomerRepositoryInterface */ private $customerRepository; public function __construct( CustomerRepositoryInterface $customerRepository ) { $this->customerRepository = $customerRepository; } /** * Gender Value * * @param $customerId * @return string|null */ public function getCustomerDob($customerId) { $dob = null; try { $customer = $this->customerRepository->getById($customerId); $dob = $customer->getDob(); } catch (NoSuchEntityException $exception) { throw new LocalizedException( __($exception->getMessage()) ); } return $dob; } }
Retrieve the DOB value by template,
$customerId = “5”;
echo $countryName = $this->getCustomerDob($customerId);
Output:
1990-10-10 (If Dob is set from the backend)