How get current year from last year in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The Date is an inbuilt function used to format the timestamp. The computer stores date and time in UNIX Timestamp. This time is measured as a number of seconds since Jan 1, 1970. Since this is impractical for humans to read, PHP converts timestamp to a format that is readable and more understandable to humans.

    Syntax:

    date( format, timestamp )

    Explanation:

    • The format parameter in the date() function specifies the format of returned date and time.
    • The timestamp is an optional parameter. If it is not included then the current date and time will be used.

    In order to get the current year as four digits, Y is used as a parameter to date() function as shown in the below:

    Program:

    echo "Current Year is :"

    $year = date("Y"); 

    echo $year

    ?> 

    Output:

    Current Year is :2019
    

    Format options available in date() function: The format parameter of the date() function is a string that can contain multiple characters allowing to generate dates in various formats are listed below:

    • d – Represents the day of the month. It uses two digits with leading zeros (01 or 31).
    • D – Represents day of the week in the text (Mon to Sun).
    • m – Represents month in numbers with leading zeros (01 or 12).
    • M – Represents month in text, abbreviated (Jan to Dec).
    • y – Represents year in two digits (08 or 14).
    • Y – Represents year in four digits (2008 or 2014).

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

    Topic: PHP / MySQLPrev|Next

    Answer: Use the PHP date() Function

    You can simply use the PHP date() function to get the current year.

    The following example code will be very useful if you want to put a copyright notice in a website footer without worrying about changing it every year.

    Copyright © MyWebsite. All Rights Reserved.


    Here are some more FAQ related to this topic:

    • How to extract substring from a string in PHP
    • How to combine two strings in PHP
    • How to convert a string to uppercase in PHP

    In this tutorial, we are going to learn about how to get the previous year in PHP.

    Getting the previous year

    To get the previous year in PHP. first, we need to access the current year using the built-in date("Y") function then we need to subtract it with minus 1.

    The date("Y") function returns the current year in four-digit(2021) string format according to the user’s local time.

    Here is an example:

    $currentYear = date("Y");
    $previousYear = $currentYear-1;
    
    echo $previousYear;

    Output:

    2020

    In this tutorial, we will show you how to get the current year using PHP.

    There are a number of different ways to do this, so we will include a few examples.

    Get the current year using PHP’s date function.

    To get the current year using PHP’s date function, you can pass in the “Y” format character like so:

    //Getting the current year using
    //PHP's date function.
    $year = date("Y");
    echo $year;
    

    The example above will print out the full 4-digit representation of the current year.

    If you only want to retrieve the 2-digit format, then you can use the lowercase “y” format character instead:

    $year = date("y");
    echo $year;

    The snippet above will print out 22 instead of 2022, or 19 instead of 2019, etc.

    Using the DateTime object.

    If you want to use a more object-oriented approach, then you can use PHP’s DateTime object:

    //Getting the current year
    //using DateTime.
    $currentDate = new DateTime();
    //Get the year by using the format method.
    $year = $currentDate->format("Y");
    //Print it out
    echo $year;

    Here, we created a DateTime object.

    We then retrieved the current year by passing the “Y” character in as the $format parameter.

    To get the 2-digit format instead, you can use the lowercase “y” character:

    //Getting the two-digit format of
    //the current year using DateTime
    $currentDate = new DateTime();
    $year = $currentDate->format("y");
    echo $year;

    As you can see, both the date function and the DateTime object use the exact same “format characters”.

    Related articles:

    • Get last year’s date using PHP.
    • Get next year’s date using PHP.

    How can get current year start and end date in PHP?

    $year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);

    How can I get current year in laravel 8?

    Laravel Carbon Get Year from Date Example.
    Get Year from Date: use Carbon\Carbon; class SignaturePadController extends Controller. $myDate = '12/08/2020'; ... .
    Output: string(4) "2020".
    Get Current Year: use Carbon\Carbon; class SignaturePadController extends Controller. $date = Carbon::now()->format('Y');.

    How can I get current date and previous date in PHP?

    php $m = date("m"); // Month value $de = date("d"); // Today's date $y = date("Y"); // Year value echo "Yesterday's date was: " . date('d-m-Y', mktime(0,0,0,$m,($de-1),$y)); ?>

    What does date () do in PHP?

    The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.