strange math error formatting date?

So obviously I’m trying to generate a date that is recurring every 7 days for x number of weeks, it’s working every time the previous month had 31 days in it however when it runs after a month with 30, the generated day ends up being the day before the one it should be… I know if I run this math outside of php it works.

So if anyone has any idea why I might be losing a day for certain months I’d appreciate it!

[php]
while ($i <= $num_weeks) {
$month = date(m);
$year = date(Y);
if ($_POST[‘date’] == “”) {
$date = date(“Y-m-d”);
if ($i > 0) {
$change_week = ($i * 7);
$new_day = (date(d) + $change_week);
$num_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if ($new_day > $num_days) {
while ($new_day > $num_days) {
$month++;
if ($month > 12) {
$year++;
$month++;
}
$num_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$new_day = ($new_day - $num_days);
}
$day = $new_day;
} else {
$day = $new_day;
}
if (strlen($day) == 1) {
$day = (“0” . $day);
}
if (strlen($month) == 1) {
$month = (“0” . $month);
}
$date = ($year . “-” . $month . “-” . $day);
}
}
}
[/php]

I’m subtracting the wrong month aren’t I…

Well, I would just use the date functions along with the string-to-time functions. Then, you just have a simple command to loop thru using a simple for loop. Just use “+ 7 days” in the string-to function…
Like: $date7 = strtotime("+7 days"); And, loop thru adding 7, 14, 21, etc…

Here is a link that explains it all:
http://php.net/manual/en/function.strtotime.php

Sponsor our Newsletter | Privacy Policy | Terms of Service