Get the year from specified date php

Get the year from specified date php

Asked on December 27, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Actually, the DateTime class internally does not use any date functions like unix timestamp,So the date is deal with before 1970 or after 2038.

    $date = DateTime::createFromFormat("Y-m-d", "2068-06-15");
    echo $date->format("Y");
    
    Answered on December 27, 2018.
    Add Comment

    The strtotime and date functions can be used for get the year from specified date,

    echo date('Y', strtotime('2068-06-15'));
    

    Till the year of 2038 , php can handle year.

    Incase the date is always in that particular format, we can get the year like this example.

    $parts = explode('-', '2068-06-15');
    echo $parts[0];
    
    Answered on December 27, 2018.
    Add Comment

    It is very simple, just use this code for get the year from specified date in php.

    <?php
    list($year) = explode("-", "2068-06-15");
    echo $year;
    ?>
    
    Answered on December 27, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.