/    Sign up×
Community /Pin to ProfileBookmark

how to accomplish this ?

Hello forums!!
Suppose consider the following case:
I had the following date range:
From: 2006-05-10
To: 2007-08-15
Q’s?
1> How to find the no of saturdays within the range with appropriate date format ie YYYY-mm-dd .

2> How to find the first and last saturday of each month within the range ?

Thanks in advance for the kind help.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyApr 03.2008 — So us what you have attempted for your assignment/homework and we may be able to assist you, we won't, however, just provide you with an answer.
Copy linkTweet thisAlerts:
@MrCoderApr 03.2008 — Yeah, I have totally given up on helping this guy, he never replies after people have gone to the trouble of helping him.
Copy linkTweet thisAlerts:
@PHPychoauthorApr 03.2008 — I have found this and its great.. really works superbly.
[code=php]<?php
// convert to timestamp
$start = strtotime( '2006-05-10 00:00:00');
$end = strtotime( '2007-08-15 00:00:00');

// initialize an array to hold the saturdays
$saturdays = array();
$monthSaturdays = array();

// this is for question 1
for ( $i = $start; $i <= $end; $i += 86400 ) {
if ( (int)date( 'w', $i ) == 6 ) {
// store the saturdays
$saturdays[] = date( 'Y-m-d', $i );
}
}

// this is for question 2
for ( $i = $start; $i <= $end; $i += 86400 ) {
$currentMonth = date( 'Y-m', $i );
if ( ! isset( $monthSaturdays[$currentMonth] ) ) {
$monthSaturdays[$currentMonth] = array( null, null );
}
// store the first Saturday of the month (index 0 will hold the firs, index 1 will hold the last)
if ( ! isset( $monthSaturdays[$currentMonth][0] ) && (int)date( 'w', $i ) == 6 ) {
$monthSaturdays[$currentMonth][0] = date( 'Y-m-d', $i );

}
// the last Saturday will be automatically set
if ( (int)date( 'w', $i ) == 6 ) {
$monthSaturdays[$currentMonth][1] = date( 'Y-m-d', $i );
}
}

?>[/code]
×

Success!

Help @PHPycho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...