PHP - How to get the first day of the current year?

By IncludeHelp Last updated : January 10, 2024

Problem statement

Write a PHP script to get the first day of the current year.

Getting the first day of the current year

To get the first day of the current year, use the mktime() function with the values 0, 0, 0, 1, 1, and date("Y") of hour, minute, second, month, day, and year respectively. Then use the date() function to format the first day as a date string.

Note

To pass the current year in the mktime() function, use the date("Y").

PHP script to get the first day of the current year

The below code will print the first day of the current year.

<?php
// Getting the first day (date) of the current year
$first_day = mktime(0, 0, 0, 1, 1, date("Y"));

// Formatting it as a date string
$result = date("d-m-Y", $first_day);
echo "The first day (date) of the year is: " . $result;
?>

Output

The output of the above code is:

The first day (date) of the year is: 01-01-2024

To understand the above program, you should have the basic knowledge of the following PHP topics:

More PHP File Handling Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.