gijew Posted August 11, 2008 Share Posted August 11, 2008 I'm trying to write up something to count the amount of days since a known datetime field. I'm getting stuck figuring out how many weekends are in that time period (as I don't want to include them). I'm not used to doing these kind of time calculations so I would love some help here. Thanks! <?php $today = time(); $start_date = mktime(0,0,0,12, 25, 2006); # just threw in a random date $difference = $today - $start_date; if ($difference < 0) { $difference = 0; } $time_since_start_date = floor($difference/60/60/24); ?> Link to comment https://forums.phpfreaks.com/topic/119209-solved-count-the-amount-of-days-since-x-minus-weekends/ Share on other sites More sharing options...
effigy Posted August 11, 2008 Share Posted August 11, 2008 Does this work? <pre> <?php function weekdays_since($date) { $from_date = strtotime($date); $to_date = time(); $days = 0; while ($from_date < $to_date) { $from_date = strtotime('+1 day', $from_date); $day = date('D', $from_date); if ($day == 'Sat' || $day == 'Sun') { continue; } ++$days; } return $days; } echo weekdays_since('01/01/2008 12:00 AM'); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/119209-solved-count-the-amount-of-days-since-x-minus-weekends/#findComment-614009 Share on other sites More sharing options...
gijew Posted August 11, 2008 Author Share Posted August 11, 2008 Fantastic!!! I have to keep that strtotime() function stuck in my head. It's apparently much more valuable than I had given it credit for Thanks again effigy. First rate all the way brotha! Link to comment https://forums.phpfreaks.com/topic/119209-solved-count-the-amount-of-days-since-x-minus-weekends/#findComment-614015 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 This may require a little bit of double checking, but it does it in a fairly simple way. It makes the assumption that beyond 1 week being 5 days ( minus weekends ) the only outside factors that will affect the outcome are if the start or end days are on a weekend themselves. <?php $today = time(); $start_date = mktime(0,0,0,12, 15, 2006); # just threw in a random date $diff = $today - $start_date; $days = floor( $diff/86400 ); $startDay = date( 'w', $start_date ); $endDay = date( 'w', $today ); # Check to see if start is on a weekend if ( $startDay == 6 ) # Saturday $days -= 2; elseif ( $startDay == 0 ) # Sunday $days -= 1; # Check to see if end is on a weekend if ( $endDay == 6 ) # Saturday $days -= 2; elseif ( $endDay == 0 ) # Sunday $days -= 1; $weeks = $daysWithoutWeekends = floor( $days/7 ) * 5; echo $daysWithoutWeekends; ?> Link to comment https://forums.phpfreaks.com/topic/119209-solved-count-the-amount-of-days-since-x-minus-weekends/#findComment-614016 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.