Get year from date php

I have a date in this format 2068-06-15. I want to get the year from the date, using php functions. Could someone please suggest how this could be done.

wattostudios

8,59613 gold badges42 silver badges57 bronze badges

asked Dec 25, 2010 at 7:44

Shakti SinghShakti Singh

82.1k20 gold badges132 silver badges150 bronze badges

1

$date = DateTime::createFromFormat("Y-m-d", "2068-06-15");
echo $date->format("Y");

The DateTime class does not use an unix timestamp internally, so it han handle dates before 1970 or after 2038.

answered Dec 25, 2010 at 7:57

0

You can use the strtotime and date functions like this:

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

Note however that PHP can handle year upto 2038

You can test it out here


If your date is always in that format, you can also get the year like this:

$parts = explode('-', '2068-06-15');
echo $parts[0];

answered Dec 25, 2010 at 7:47

Get year from date php

SarfrazSarfraz

371k74 gold badges529 silver badges575 bronze badges

6

I would use this:

$parts = explode('-', '2068-06-15');
echo $parts[0];

It appears the date is coming from a source where it is always the same, much quicker this way using explode.

Get year from date php

CuberChase

4,4205 gold badges32 silver badges52 bronze badges

answered Jul 10, 2013 at 23:09

Get year from date php

carninicarnini

651 silver badge7 bronze badges

You can try strtotime() and date() functions for output in minimum code and using standard way.

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

output: 2068

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

output: 68

answered Jun 4, 2020 at 11:37

Get year from date php

ShwetaShweta

5515 silver badges11 bronze badges

  public function getYear($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("Y");
}

public function getMonth($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("m");
}

public function getDay($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("d");
}

answered Jan 21, 2014 at 13:13

Get year from date php

HibaHiba

1011 silver badge2 bronze badges

 list($year) = explode("-", "2068-06-15"); echo $year; ?> 

answered Dec 25, 2010 at 8:17

fvoxfvox

1636 bronze badges

You can achieve your goal by using php date() & explode() functions:

$date = date("2068-06-15");

$date_arr = explode("-", $date);

$yr = $date_arr[0];

echo $yr;

That is it. Happy coding :)

answered Feb 12, 2020 at 6:08

Rashed RahatRashed Rahat

2,0501 gold badge17 silver badges32 bronze badges

Assuming you have the date as a string (sorry it was unclear from your question if that is the case) could split the string on the - characters like so:

$date = "2068-06-15";
$split_date = split("-", $date);
$year = $split_date[0];

answered Dec 25, 2010 at 7:49

dshipperdshipper

3,4193 gold badges29 silver badges41 bronze badges

1

$Y_date = split("-","2068-06-15");
$year = $Y_date[0];

You can use explode also

answered Nov 12, 2012 at 9:39

V A SV A S

3,2094 gold badges33 silver badges38 bronze badges

You wrote that format can change from YYYY-mm-dd to dd-mm-YYYY you can try to find year there

$parts = explode("-","2068-06-15");
for ($i = 0; $i < count($parts); $i++)
{
     if(strlen($parts[$i]) == 4)
     {
          $year = $parts[$i];
          break;
      }
  }

answered Jul 10, 2013 at 23:32

bidonbidon

32 bronze badges

How to get the year of a date in PHP?

$date = DateTime::createFromFormat("Y-m-d", "2068-06-15"); echo $date->format("Y");

How to get year from DateTime object in PHP?

Method 1: Using only date() function to get current year It can take two parameters. If you use only one parameter, It will return info about the current time. echo "4 digit of current year is: " . date ( "Y" );

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 to get current date in PHP With time?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.