2

I'm getting a weird date ('01/01/1970 00:00:00) when I run the following code in php:

$now = date('d/m/Y H:i:s', time());
$exp = date('d/m/Y H:i:s', strtotime($now .'+100 years'));
  • I believe time goes up to 2099 with strtotime. Use `DateTime` class and `format`method. – unalignedmemoryaccess Jun 26 '17 at 13:23
  • It's possible you are running into this issue: https://en.wikipedia.org/wiki/Year_2038_problem – Doug Jun 26 '17 at 13:26
  • [strtotime](http://php.net/manual/en/function.strtotime.php) does not like your format: `Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.` – aynber Jun 26 '17 at 13:30

8 Answers8

6

I would try something like this

$date = new DateTime();
$exp = $date->modify('+100 years')->format('d/m/Y H:i:s');
Ivan G.
  • 146
  • 2
3

Try this code :

<?php
$year = date('Y-m-d H:i:s', strtotime('+5 years'));
echo $year;
?>

Output:

2022-06-26 13:29:07

RaMeSh
  • 3,330
  • 2
  • 19
  • 31
1

Try following one.

$now = date('d/m/Y H:i:s', strtotime('+100 years'));

Output => 26/06/2117 18:58:07

Mahesh Gaikwad
  • 192
  • 2
  • 18
0

Try this

date('d/m/Y H:i:s', strtotime('+100 years'));

Output :-

26/06/2117 09:31:15
Narayan
  • 1,670
  • 1
  • 19
  • 37
0

It because you're giving the wrong date format for this function strtotime($now .'+100 years') and it returns false.

try that:

echo date('d/m/Y H:i:s', strtotime("+100 year"));
buildok
  • 785
  • 6
  • 7
0

"The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)"

http://php.net/manual/en/function.strtotime.php

Bernhard
  • 1,852
  • 11
  • 19
0
$d = date('Y-m-d', strtotime('+5 years'));
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
0

You're apparently falling into one or both the following problems:

  1. a simple formatting issue (this is wrong for sure) where, since you're using / as separator, strtotime interprets days as months and vice-versa; as you can read in the documentation page for strtotime (Notes paragraph):

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed

This can be easily fixed by changing your first line from:

$now = date('d\m\Y H:i:s', time());

to:

$now = date('d-m-Y H:i:s', time());
  1. the Year 2038 Problem. You can read about this too, in the same documentation page (same paragraph, right above the previous quote):

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer)

If that's your case, you better use DateTime objects (as others suggested here), which do not suffer this limitation. Here's a line which should work:

$exp = date_format(date_create("+100 years"), 'd/m/Y H:i:s');

What is happening in your code is that:

  • since you're passing a non-existing date (and maybe because of a 32-bit environment too), strtotime returns false
  • when passing false to the second argument of the date function, which expects an int, false is cast to 0, which makes date return the Epoch Time.
mapofemergence
  • 458
  • 3
  • 7
  • ups, I got lost trying to make the answer clear enough and did not notice that so many had already replied :/ – mapofemergence Jun 26 '17 at 15:01
  • Anyway, my answer is in line with what @aynber hinted about formatting + what [Bernhard](https://stackoverflow.com/a/44761257/8200213) and [tilz0R](https://stackoverflow.com/questions/44760770/add-years-to-todays-date-in-php/44762681#comment76502249_44760770) said about the 32-bit limit. My solution is very similar to [Ivan G's](https://stackoverflow.com/a/44760826/8200213). I'm still quite new on Stack Overflow: if my reply is redundant let me know and I'll delete it – mapofemergence Jun 26 '17 at 15:20