Jump to content

[SOLVED] Count the amount of days since (x) minus weekends


gijew

Recommended Posts

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);
?>

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>

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;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.