Jump to content

Help counting the number of a curtain day/s between two dates


bodycount

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.