2
$date_raw = '05/05/1995';
$newDate = (date('j F Y', strtotime('-192years -14months -2days', strtotime($date_raw))));
print "New Date: $newDate <br>";

I'm trying to subtract 100+ years from a given date. but the value i get is real till 92 years only. after that i don't get correct subtraction. whats the reason?

  • 1
    The simple reason is: `UNIX timestamp` – Rizier123 Apr 14 '15 at 09:11
  • http://stackoverflow.com/questions/1990321/date-minus-1-year – Soorajlal K G Apr 14 '15 at 09:17
  • 1
    If you need to work with dates that fall outside the range of a 32-bit signed unix timestamp (1901-12-13 to 2038-01-19), then start using [DateTime objects](http://www.php.net/manual/en/class.datetime.php) – Mark Baker Apr 14 '15 at 09:17
  • `$date_raw = '05/05/1995'; $newDate = (new DateTime($date_raw)) ->sub(new DateInterval('P192Y14M2D')) ->format('j F Y'); print "New Date: $newDate
    ";`
    – Mark Baker Apr 14 '15 at 09:27
  • any example please?? I'm new to PHP and i did my best to solve this problem. i will be grateful to you if u produce an example for me please. – Kumar Satish Apr 14 '15 at 09:27

2 Answers2

2

If you need to work with dates that fall outside the range of a 32-bit signed unix timestamp (1901-12-13 to 2038-01-19), then start using DateTime objects

$date_raw = '05/05/1995';
$newDate = (new DateTime($date_raw))
    ->sub(new DateInterval('P192Y14M2D'))
    ->format('j F Y');

echo $newDate, PHP_EOL;

gives

3 March 1802
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • thanks. accepted this as answer. by the way my question does not deserve to be voted down. i made a deep research about my problem on the internet before i came here. but whoever did this must be biased to newcomers. – Kumar Satish Apr 14 '15 at 11:53
0

or you can do this in the following way

// set your date here

$mydate = "2018-06-27";

/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */

$lastHundredyear = strtotime("-100 year", strtotime($mydate));

// format and display the computed date

echo date("Y-m-d", $lastHundredyear);

this will give you following output

1918-06-27
saadk
  • 1,243
  • 14
  • 18