DateTime Class不支持1960年以前的年份?

I have the following situation:

$birth = new DateTime('1960');
echo $birth->diff( new DateTime() )->format('%y');

The output is 56 - that is OK, but when I set an argument 1959 or less value I got result 0. For example:

$birth = new DateTime('1959');
echo $birth->diff( new DateTime() )->format('%y');

The output is 0.

The test environment is:

  • PHP 5.5.9-1ubuntu4.17
  • Apache/2.4.10 (Ubuntu)

Thanks!

You need to specify a valid DateTime for the input to new DateTime().

I guess that 1960 by itself can only be interpreted as a year, while 1959 could be a time. Printing out the difference in years between the time now and the time at 19:59 is 0.

You would be better off specifying a full date, like 1959-01-01.