1

I need to convert year(YY) to YYYY. Here i am try to use date function in PHP but not getting the expected result.

$expiry_year = "30"; //year=2030
echo date('Y',strtotime($expiry_year)); //Result: 1970
echo date('Y',$expiry_year); //Result: 1970

Expected Result: 2030

Thanks To All!

Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46

4 Answers4

5

Try this, use createFromFormat

$date = "30";
$dates = DateTime::createFromFormat('y', $date);
//now to get the outpu:
$arr = $dates->format('Y'); // output : 2030

DEMO

Dave
  • 3,073
  • 7
  • 20
  • 33
1

Note: If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).

For this you can use date_create_from_format Alias of the DateTime class createFromFormat method like as

$expiry_year = "30";
$date = date_create_from_format('y',$expiry_year);
echo $date->format('Y'); //2030

Docs

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1

Try this :

$date = DateTime::createFromFormat('y', '30');
echo $date->format('Y');
Steven
  • 313
  • 3
  • 10
-1
$expiry_year = "30";
$ce = substr(date('Y'),0,2); // Check the century
$ny = substr(date('Y'),2,2); // Check current year (2 digits)
if($expiry_year < $ny) {
    $next_ce = $ce+1;
    $output = $next_ce.$expiry_year;
} else {
    $output = $ce.$expiry_year;
}

Something like that ? :D

Jicao
  • 96
  • 2
  • 11