Determine week number based on starting date
I need help to create a function to determine the week number based on these 2 parameters:
- Starting date
- Specified date
For example, if I specify April 7, 2010 as the starting date & passed April 20, 2010 as the date to lookup, I would like the function to return WEEK 2. Another example, if I set March 6, 2010 as starting date and looked up April 5, 2010 then it should return WEEK 6.
I appreciate your time and help.
=================
UPDATE
For instance:
Starting date: 3/6 - week1 Lookup date: 4/5
Week 2 starts from Mar 7-13. Week 3 (3/14-3/20). Week 4 (3/21-3/27). Week 5 (3/28-4/3). So 4/5 falls under Week 6.
An idea is t开发者_如何学Goo use the Sunday of the starting date as the NEW* starting date. So instead of looking up 3/6, the function will use 2/28 as the starting date.
You can find the number of days between the two dates, divide that number by 7
and take the ceil
of the result:
$start_date = strtotime("2010-04-07");
$specified_date = strtotime("2010-04-20");
$num_of_days = ($specified_date - $start_date)/(60*60*24);
$week_num = ceil($num_of_days/7);
echo $week_num; // prints 2.
What about?
- Determine the number of days since the epoch of the start date
- ditto, the specified date
- subtract
- divide by 7
Since I do not know how you are going to receive your dates, I would do something like:
$starting = mktime(parsedStartingDate);
$lookup = mktime(parsedLookupDate);
$result = $lookup - $starting;
$result = (((($result / 60) / 60) / 24) / 7);
return $result;
Since you're not being very clear this may or may not be what you're looking for:
// Example 1 (returns 2)
date('W', strtotime('April 20, 2010')) - date('W', strtotime('April 7, 2010'));
// Example 2 (returns 5)
date('W', strtotime('April 5, 2010')) - date('W', strtotime('March 6, 2010'));
Figured it out.
$start_date = strtotime("2010-03-06"); // returns 1267862400
$start_date_week = strtotime("last sunday",$start_date); // 02-28
$specified_date = strtotime("2010-04-10"); // returns 1270882800
$specified_date_week = strtotime("next sunday",$specified_date); // 4-11 looking up the Next Sunday was also the key!
$num_of_days = ($specified_date_week - $start_date_week)/(60*60*24); // 41.958
$week_num = ceil($num_of_days/7); //6
echo $week_num;
精彩评论