Page 1 of 1
Help counting the number of a curtain day/s between two date
Posted: Wed Apr 14, 2010 10:18 am
by bodycount
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.
Code: Select all
<?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
Re: Help counting the number of a curtain day/s between two
Posted: Wed Apr 14, 2010 11:21 am
by AbraCadaver
This will do Saturdays:
Code: Select all
for($i=-1, $next=strtotime('-1 day', strtotime($digest_date)); $next<=strtotime($todaysdate); $i++, $next=strtotime('next Saturday', $next));
echo $i;
It is better to use time() if it will always be todays date:
Code: Select all
for($i=-1, $next=strtotime('-1 day', strtotime($digest_date)); $next<=time(); $i++, $next=strtotime('next Saturday', $next));
echo $i;
And you could strtotime() before the loop for the $digest_date:
Code: Select all
$start = strtotime($digest_date);
for($i=-1, $next=strtotime('-1 day', $start); $next<=time(); $i++, $next=strtotime('next Saturday', $next));
echo $i;
Maybe a function:
Code: Select all
function days_between($day, $start, $stop=null) {
if(empty($stop)) {
$stop = time();
}
if(!is_int($start) && !($start = strtotime($start))) {
return false;
}
if(!is_int($stop) && !($stop = strtotime($stop))) {
return false;
}
for($i=-1, $next=strtotime("previous $day",$start); $next<=$stop; $i++, $next=strtotime("next $day", $next));
return $i;
}
echo days_between('Thursday', '2010-04-08', '2010-04-15');
Re: Help counting the number of a curtain day/s between two
Posted: Wed Apr 14, 2010 12:00 pm
by Eran
Abra, your function seems to return incorrect values most of the time. Try those dates with 'sunday' and 'monday' for example.
Here is a function that achieves a similar result with date functions instead of a loop:
Code: Select all
$digest_date = "2010-04-04";
$todaysdate = "2010-04-19";
$wednesdays = weekdayCount(4,$digest_date,$todaysdate);
echo $wednesdays;
function weekdayCount($weekday,$start,$end = null) {
$start = !is_numeric($start) ? strtotime($start) : $start;
$end = !is_numeric($end) ? (empty($end) ? time() : strtotime($end)) : $end;
$edges = (((date('w',$end) < $weekday - 1) ? 1 : 0) + ((date('w',$start) > $weekday - 1) ? 1 : 0));
return (int)date('W',$end - $start) - $edges;
}
Re: Help counting the number of a curtain day/s between two
Posted: Wed Apr 14, 2010 2:26 pm
by AbraCadaver
pytrin wrote:Abra, your function seems to return incorrect values most of the time. Try those dates with 'sunday' and 'monday' for example.
Good catch. I left time() in the function loop when I copy pasted by mistake. Also, in all examples I needed to back off 1 day or 1 week because it wasn't counting if the start date was actually that day. Edited above.
Re: Help counting the number of a curtain day/s between two
Posted: Wed Apr 14, 2010 2:56 pm
by cpetercarter
This is clearly a favourite college assignment.
viewtopic.php?f=1&t=114078
Re: Help counting the number of a curtain day/s between two
Posted: Wed Apr 14, 2010 2:58 pm
by AbraCadaver
Haha! At least bodycount gave it a shot and wrote some code.