89

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.

1
  • Yes, I have maximum input 2069 Commented Dec 25, 2010 at 7:54

10 Answers 10

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

Sign up to request clarification or add additional context in comments.

Comments

79

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

6 Comments

I already tried this but it is giving me 1970 that it is why php can't read the year above the 2038. right?
@Shakti: Because as i have said in my answer, PHP can handle year up to 2038 and you have specified 2068. You can test it here: codepad.org/iwqtML6s
ya, I agree with you but I have maximum input 2069. so I think I have to use substr or similar function which can split it but the problem is input date fromat can be changed now it is yyyy-mm-dd may be later changed to mm-dd-yyyy
@Shakti: As i have shown in my answre later, you can get the date with explode function too: $parts = explode('-', '2068-06-15'); echo $parts[0];. You can test it here: codepad.org/HbS63y2n
I do not understand why you're using unix timestamps when the OP has given an example outside of it's range.
|
10

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

Comments

5

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.

Comments

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

Comments

2

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

Comments

2

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

Comments

0

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

1 Comment

Note that split is deprecated.
0
$Y_date = split("-","2068-06-15");
$year = $Y_date[0];

You can use explode also

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.