Extract the Day / Month / Year from a Timestamp in PHP MySQL?

To extract the Day/Month/Year from a timestamp in PHP, you can use the date_parse() function. This function parses a date string and returns an associative array with detailed date and time components.

Syntax

print_r(date_parse("anyTimeStampValue"));

Example

Let's extract day, month, and year from a timestamp using date_parse() ?

<?php
$yourTimeStampValue = "2019-02-04 12:56:50";
$parsedDate = date_parse($yourTimeStampValue);
print_r($parsedDate);
?>
Array
(
    [year] => 2019
    [month] => 2
    [day] => 4
    [hour] => 12
    [minute] => 56
    [second] => 50
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )
    [error_count] => 0
    [errors] => Array
        (
        )
    [is_localtime] =>
)

Accessing Individual Components

You can access specific components directly from the parsed array ?

<?php
$yourTimeStampValue = "2019-02-04 12:56:50";
$parsedDate = date_parse($yourTimeStampValue);

echo "Year: " . $parsedDate['year'] . "<br>";
echo "Month: " . $parsedDate['month'] . "<br>";
echo "Day: " . $parsedDate['day'] . "<br>";
?>
Year: 2019
Month: 2
Day: 4

Alternative Method Using date() Function

You can also use the date() function with strtotime() for simpler extraction ?

<?php
$timestamp = "2019-02-04 12:56:50";
$time = strtotime($timestamp);

echo "Year: " . date('Y', $time) . "<br>";
echo "Month: " . date('m', $time) . "<br>";
echo "Day: " . date('d', $time) . "<br>";
?>
Year: 2019
Month: 02
Day: 04

Conclusion

The date_parse() function provides comprehensive date parsing with error handling, while date() with strtotime() offers a simpler approach for basic date component extraction.

Updated on: 2026-03-15T08:07:57+05:30

967 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements