Forum Moderators: coopster

Message Too Old, No Replies

How many Sundays in a date range?

Surely a simpler way...

         

Artie_J

3:47 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



Dates come in the format 2006-02-16 (year-month-day) for example.
I have this rather complicated proceedure using explode(), then mktime() that will give me the number of days in the date range, which could then be used in a loop that would step through the days. Then I would need to go back from mktime format to XXXX-XX-XX format (not sure about that part yet), REEXPLODE the darn thing in order to use jddayofweek(gregoriantojd($mo, $day, $yr), 1) to get the day of the week that could be used in a counting loop.

I know this description is very general, but all of it does work (except for the format change I indicated I'm not sure how to do yet).
Just wondering if anybody knows of a more elegant solution. I'm not looking for "Here's the code", just more like "I know of this great little function or proceedure..."
Thanks in advance for any info.

JollyK

4:37 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



Basically, you don't have to explode() the string date, because using strtotime('2006-02-16') gives you a timestamp which is the number of seconds since the epoch. Then, you can use that timestamp with "date()" or in a loop kind of like this:

$date1 = '2006-02-06';
$date2 = '2006-02-28';

for ($i = 0; $i < ((strtotime($date2) - strtotime($date1)) / 86400); $i++){
if(date('l',strtotime($date1) + ($i * 86400)) == 'Sunday'){
$num_sundays++;
}
}
echo "There are $num_sundays Sundays in that date range.\n";

"86400" is because that's 24 hours, and hence, "a day."

strtotime($later_date) - strtotime($earlier_date) should give you the total number of seconds between those two dates. Dividing that by 86400 should give you the number of days between those dates. (NOTE: this will ONLY work if you are using YYYY-MM-DD as the argument to strtotime() because it sets them both to 00:00:00 for the time and it will be exactly a multiple of 24 hours apart. If you also give it times, and the times are different, then you'll have to make sure to integerize the result of the division.)

In the loop, you move the date up by starting with $date1, and adding "$i * 86400" so that as $i goes up, the date goes up by one day/24 hours/86400 seconds.

Then, date('l',[timestamp]) (that's a letter "l" not a number one, btw) gives the full day of the week, so you just check it for "Sunday" in the loop.

Does that make sense?

I love strtotime(). :-)

I'm sure a PHP guru could give a better answer, and there may be something wrong with the above (PHP is not my first language), but it seemed to test out okay and give me the valid number of Sundays when I gave it different dates ...

JK

Artie_J

12:49 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



Awesome, works beauty!
I understand it a lot better thanks to your explaination.
When I go to php.net, the function explainations are rather dry and jargon filled, and then the user additional comments are usually over my head! Guess I just need to find a good learning source that speaks down to my level, haha.

Yep, strtotime() is great!

JollyK

3:22 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



Heh. Glad I could help. Yeah, the manual pages can be a bit dry. I wish they'd give more examples of usage sometimes, since that makes more sense to me.

JK

trillianjedi

3:28 pm on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wish they'd give more examples of usage sometimes

I often find the user contributed "notes" at the end far more useful that the function definition or description.

JollyK

8:26 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



trillianjedi: You've got that right! Although sometimes the user notes are pretty sparse, depending on the function/feature.

Maybe I should go post the above code snippet there under strftotime(). :-)

JK