bodycount Posted April 14, 2010 Share Posted April 14, 2010 Hi all I have been banging my head against a wall looking all over the web to find how to calculate how many saturdays and how many wednesdays that have passed between two dates, but all I can find on the web is how to calculate the amount of days between two dates. <?php $digest_date = "2010-04-05"; $todaysdate = "2010-04-19"; $date_diff = round( abs(strtotime($todaysdate))-strtotime($digest_date)) / 86400, 0 )); echo "$date_diff"; ?> Is there away to do it? Thanks Link to comment https://forums.phpfreaks.com/topic/198519-help-counting-the-number-of-a-curtain-days-between-two-dates/ Share on other sites More sharing options...
aeroswat Posted April 14, 2010 Share Posted April 14, 2010 Get the days between two dates in an array http://edrackham.com/php/get-days-between-two-dates-using-php/ use a loop with a counter to find out if each date is either a wednesday or a saturday you can use strtotime($date) to get the time and then use the date function with date string of lowercase L i believe Link to comment https://forums.phpfreaks.com/topic/198519-help-counting-the-number-of-a-curtain-days-between-two-dates/#findComment-1041691 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Between inclusively or exclusively? So say today is Saturday and I go one week from now to next week Saturday. Is that 0, 1 or 2? Link to comment https://forums.phpfreaks.com/topic/198519-help-counting-the-number-of-a-curtain-days-between-two-dates/#findComment-1041692 Share on other sites More sharing options...
bodycount Posted April 14, 2010 Author Share Posted April 14, 2010 "Between inclusively or exclusively? So say today is Saturday and I go one week from now to next week Saturday. Is that 0, 1 or 2?" inclusively so there would be 2 saturdays and 1 wednesday Link to comment https://forums.phpfreaks.com/topic/198519-help-counting-the-number-of-a-curtain-days-between-two-dates/#findComment-1041704 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Not tested, but try this. <?php // temp vars to count the number of saturdays and wednesdays $num_saturdays = 0; $num_wednesdays = 0; // date to timestamp $time1 = mktime(0, 0, 0, 4, 5, 2010); $time2 = mktime(0, 0, 0, 4, 19, 2010); // the number of days between the two timestamps $diff_days = ($time2 - $time1) / 86400; // number of weeks between the days $num_saturdays = $num_wednesdays = ($diff_days / 7); // the day of the week for each time $day1 = date("w", $time1); $day2 = date("w", $time2); if ($day1 == 3) $num_wednesdays++; else if ($day1== 6) $num_saturdays++; // if the day of the week are not the same ... (don't count it twice) if ($day1 != $day2) { if ($day2 == 3) $num_wednesdays++; else if ($day2 == 6) $num_saturdays++; } echo sprintf("WED - %d\nSAT - %d\n", $num_wednesdays, $num_saturdays); Link to comment https://forums.phpfreaks.com/topic/198519-help-counting-the-number-of-a-curtain-days-between-two-dates/#findComment-1041725 Share on other sites More sharing options...
Psycho Posted April 14, 2010 Share Posted April 14, 2010 Here you go. This function will return an array with weekday names for keys and values as to the number of times that day occurs in the date range (tested): function getWeekDayCount($startDt, $endDt) { $weekdays = array(); $days = round(abs(strtotime($endDt) - strtotime($startDt)) / 86400, 0); $whole_weeks = floor($days/7); $extra_days = $days % 7; for($weekday=0; $weekday<7; $weekday++) { $dayName = date('l', strtotime("$startDt +$weekday days")); $weekdays[$dayName] = $whole_weeks + (($weekday<=$extra_days)?1:0); } return $weekdays; } $start_date = "2010-04-05"; $end_date = "2010-04-15"; $weekdays = getWeekDayCount($start_date, $end_date); print_r($weekdays); Output: Array ( [Monday] => 2 [Tuesday] => 2 [Wednesday] => 2 [Thursday] => 2 [Friday] => 1 [saturday] => 1 [sunday] => 1 ) Link to comment https://forums.phpfreaks.com/topic/198519-help-counting-the-number-of-a-curtain-days-between-two-dates/#findComment-1041749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.