I'm working on a project that produces reports on a weekly basis, so if today's date is "2013/09/05" the code has to figure out what was the date seven days ago, which is "2013/08/29", taking into account that some months are longer than others.
and then i want to run the dates against the database looking for matches ...
this is what i have done so far but its not working.
// Start date
$text = explode('/', $date); //2013/09/05
$day = $text[2];
$month = $text[1];
$year = $text[0];
$past_day = $day - 7; //-2
// determine if $past_day is negative
if ($past_day < 0){
$month = $month - 1; //08
$var = $day - 0; //2
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$var = ($num - $var); // 29
$start_date = $year."/".$month."/".$var;
}else{
$start_date = $year."/".$month."/".$past_day;
}
Thanks alot guys!
try this :)
$date = "2013/09/05";
// Start date
$text = explode('/', $date); //2013/09/05
$day = $text[2];
$month = $text[1];
$year = $text[0];
$past_day = $day - 7; //-2
// determine if $past_day is negative
if ($past_day < 0){
$month = $month - 1; //08
$var = ($past_day)*-1; //2
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$var = ($num - $var); // 29
$start_date = $year."/".$month."/".$var;
}else{
$start_date = $year."/".$month."/".$past_day;
}