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

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

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

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
)

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.