1

How to get +50 year from current year using php date function

Hello,

i have try below code for getting +50 year from current year. but date function allow to add only +20 year.

for($i = date('Y'); $i <= date('Y', strtotime('+50 year')); $i++){
    echo $i.',';
}

if i add +20 year then below OutPut show :

2014,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030,2031,2032,2033,2034,

and if i add +50 year then blank OutPut show :

Kevin G Flynn
  • 231
  • 4
  • 14

6 Answers6

3

You can use...

<?php
for($i = date('Y'); $i <= date('Y') + 50; $i++){
    echo $i.',';
}
?>
Iago
  • 1,214
  • 1
  • 10
  • 19
2

Why not just do it manually, as such:

$cur_year = date('Y');
for ($i=0; $i<=50; $i++) {
    echo $cur_year++ . ',';
}

DEMO

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • hello Mark thank you for answer but i want to add +50 year dynamically from current year. – Kevin G Flynn Jun 14 '14 at 07:11
  • @GirishPatidar Ok... but that's what this does, no? It adds 50 years to the current year... – Mark Miller Jun 14 '14 at 07:13
  • Unless suddenly years are numbered differently, somewhere between execution year and 50 years later, this would output the same and be faster. – 11684 Jun 14 '14 at 08:16
2

Despite indications to the contrary in some of the answers here, PHP can store and manipulate dates beyond 2038, but you have to use the DateTime classes and PHP >= 5.3 to do so.

From the manual:-

The date and time information is internally stored as a 64-bit number so all conceivably useful dates (including negative years) are supported. The range is from about 292 billion years in the past to the same in the future.

Which should be adequate for your needs.

The simplest way to achieve what you want is something like:-

$fiftyYearsFromNow = new \DateTime('+ 50 years');

See it working.

There are other ways, take a look around these very useful classes and get to know them. A direct conversion of your example loop may look something like:-

for($i = new \DateTime(); $i < new \DateTime('+ 50 years'); $i->add(new \DateInterval('P1Y'))){
    echo $i->format('Y') . "<br>\n";
}

See it working.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
1

PHP (and some other languages) can't store dates beyond 2038. Here's a related question: Why do timestamps have a limit to 2038?

Community
  • 1
  • 1
Dave Morrissey
  • 4,371
  • 1
  • 23
  • 31
  • PHP (and some other languages) __can__ store dates beyond 2038. See [my answer](http://stackoverflow.com/a/24217974/212940). – vascowhite Jun 14 '14 at 08:28
1

Try use OS and php with 64-bit architecture. This is a 2038 problem. Read more http://en.wikipedia.org/wiki/Year_2038_problem

CnapoB
  • 665
  • 1
  • 9
  • 16
0

Best and short cut method is, here i am taking from current year to +40 year

$value = range(date('Y'),date('Y', strtotime('+40 year')));
Anil Kumar
  • 701
  • 11
  • 28