Determine week number by start date

I need help creating a function to determine the week number based on these two parameters:

  • start date
  • Specified date

For example, if I specify April 7, 2010 as the start date and pass in April 20, 2010 as the search date, I would like the function to return WEEK 2. Another example, if I set March 6, 2010 as starting April 5 2010, then he must return WEEK 6.

I appreciate your time and help.

=================

UPDATE

For instance:

Start Date: 3/6 - Week1 Search Date: 4/5

Week 2 starts March 7-13. Week 3 (3 / 14-3 / 20). Week 4 (3 / 21-3 / 27). Week 5 (3 / 28-4 / 3). So 4/5 falls on week 6.

The idea is to use the Sundays of the start date as the new date *. So instead of looking for 3/6, the function will use 2/28 as the start date.

+2


a source to share


5 answers


You can find the number of days between two dates, divide that number by 7

and take the ceil

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.

      

+1


a source


What about?



  • Determine the number of days since the start date
  • ditto, specified date
  • subtract
  • divide by 7
+1


a source


Since I don't know how you are going to get your dates, I would do something like:

$starting = mktime(parsedStartingDate);
$lookup = mktime(parsedLookupDate);

$result = $lookup - $starting;
$result = (((($result / 60) / 60) / 24) / 7);

return $result;

      

0


a source


Since you didn't really get it, this may or may not be what you are 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'));

      

0


a source


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

      

0


a source







All Articles