How do I use PHP to get the current year?

H
Convert a date format in PHP
Correctly determine if the date string is a valid date in that format

Use either date or strftime. In this case, it doesn’t matter as a year is a year, no matter what (unless there’s a locale that formats the year differently).

Example

<?php echo date("Y"); ?>

Solution 2

$now = new DateTime();
$year = $now->format("Y");

Using class member access on instantiation (php>=5.4):

$year = (new DateTime)->format("Y");

https://stackoverflow.com/questions/64003/how-do-i-use-php-to-get-the-current-year/64097#64097

Convert a date format in PHP
Correctly determine if the date string is a valid date in that format

Add comment

Tutor Network

Share this

Learn PHP from A to Z