So, in PHP, I am trying to return the date value for the last year depending on the same day of the week. EX: (Monday) 2011-12-19 entered should return (Monday) 2010-12-20.
I just did it just at -364, but then it was unsuccessful in leap years. I came across another function:
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, this does not work when you try to enter a Sunday date, since it executes 0 (Sunday) minus 6 (Saturday by last year's date) and returns with a value of T-6. Input IE 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I am a little shy to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thank!