I have this code, and it prints out as '01/01/1970'
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("y")-18);
echo "DOB is ".date("d/m/y", $dob);
Why is the year not 18 years less than today?
I have this code, and it prints out as '01/01/1970'
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("y")-18);
echo "DOB is ".date("d/m/y", $dob);
Why is the year not 18 years less than today?
According to the manual, when you specify small y as the argument to date function, it will return two-digit representation of the current year. Since the current year is 2011, it will return 11. Subtracting 18 from it will give you a negative result, that's why mktime is resetting to the original timestamp.
Change date("y") to date("Y"), that is, replace small y with capital Y, then you will get the desired result.
The following code is imho much easier to read:
<?php
$dob = new DateTime();
printf("\nToday is %s", date_format($dob, 'D, M d Y'));
$dob->modify('-1 day');
$dob->modify('-18 year');
printf("\nToday minus %d day and %d year is %s",
-1, -18,
date_format($dob, 'D, M d Y'));
?>
Don't you agree? It is not so difficult to calculate date with PHP. Just look also at the PHP Date function for the various formats, such as Weeknumbers.