How to usedate() Function in PHP

The date() function in PHP allows you to format a date string. You can use it to extract the day, month, and year components.

PHP




<?php
  
// Get the current date
$date = date('2024-01-01');
  
// Extract day, month, and year
$day = date('d', strtotime($date));
$month = date('m', strtotime($date));
$year = date('Y', strtotime($date));
  
// Display the results
echo "Day: $day, Month: $month, Year: $year";
?>


Output

Day: 01, Month: 01, Year: 2024

How to Extract Day, Month and Year in PHP ?

Given a Date, the task is to extract day, month, and year from the date using PHP. There are various methods to extract day, month, and year, these are:

Table of Content

  • Using date() Function
  • Using DateTime Class
  • Using getdate() Function

Similar Reads

Approach 1: Using date() Function

The date() function in PHP allows you to format a date string. You can use it to extract the day, month, and year components....

Approach 2: Using DateTime Class

...

Approach 3: Using getdate() Function

The DateTime class provides an object-oriented way to work with dates and times in PHP. You can use it to extract the day, month, and year components....