Get the year from specified date php

PhpDate

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

$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.

Solution 2 - Php

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];

Solution 3 - Php

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.

Solution 4 - Php

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

Solution 5 - Php

  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");
}

Solution 6 - Php

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

Solution 7 - Php

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 :)

Solution 8 - Php

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];

Solution 9 - Php

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

You can use explode also

Solution 10 - Php

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;
      }
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShakti SinghView Question on Stackoverflow
Solution 1 - PhpMaerlynView Answer on Stackoverflow
Solution 2 - PhpSarfrazView Answer on Stackoverflow
Solution 3 - PhpcarniniView Answer on Stackoverflow
Solution 4 - PhpShwetaView Answer on Stackoverflow
Solution 5 - PhpHibaView Answer on Stackoverflow
Solution 6 - PhpfvoxView Answer on Stackoverflow
Solution 7 - PhpRashed RahatView Answer on Stackoverflow
Solution 8 - PhpdshipperView Answer on Stackoverflow
Solution 9 - PhpVineesh KalarickalView Answer on Stackoverflow
Solution 10 - PhpbidonView Answer on Stackoverflow