Forum Moderators: coopster

Message Too Old, No Replies

Figure out what week number a date falls into!

         

bubbasheeko

2:57 am on May 7, 2008 (gmt 0)

10+ Year Member



I am working on a project and I am not sure of how to solve it - I hate date calculations...

Okay...a visitor registers on the site on..say... 05/06/2008....and they have a special event on 02/23/2008.

I want to figure out the number of weeks between the two dates. So today being the 6th of May, it would fall into the 18th week of the 2008. That means there is 34 weeks until the end of the year and 02/23/2008 would be the 8th week in 2009...so 34 + 9 = 43 weeks. (i think this is right).

Now how can I figure this out in PHP?

MattAU

3:37 am on May 7, 2008 (gmt 0)

10+ Year Member



Something like this would do the trick.

$time_difference = strtotime($event) - strtotime($registration_date); // Get the number of seconds between the two dates.

$weeks_between = ceil($time_difference / (60 * 60 * 24 * 7)); // Divide the time difference by the number of seconds in a week, and round the result up.

MattAU

3:39 am on May 7, 2008 (gmt 0)

10+ Year Member



I should have also mentioned the you can use date('W',$a_date) to get the week number the date falls into. The above solution is a better way to do what you want though.

bubbasheeko

3:42 am on May 7, 2008 (gmt 0)

10+ Year Member



Hi Matt, never thought of the strtotime idea.

I ended up trying this:

// Current Date:

$current_date = date('m-d-Y') . "<br>";
$current_date_split = explode("-", $current_date);
$current_month = $current_date_split[0];
$current_day = $current_date_split[1];
$current_year = $current_date_split[2];

$BuildCurrentDate = mktime(0, 0, 0, $current_month, $current_day, $current_year);
$current_week = date('W', $BuildCurrentDate) . "<br>";

=> OUTPUT '19'

// Future Date:
$make_future_date = "02-28-2009";

$future_date_split = explode("-", $make_future_date);

$future_month = $future_date_split[0] . "<br>";
$future_day = $future_date_split[1] . "<br>";
$future_year = $future_date_split[2] . "<br>";

$BuildFutureDate = mktime(0, 0, 0, $future_month, $future_day, $future_year);
$FutureWeek = date('W', $BuildFutureDate);

=> OUTPUT IS '9'

Of course that's all fine and 'dandy', but I can't simply do a calculation on these numbers. lol

bubbasheeko

3:52 am on May 7, 2008 (gmt 0)

10+ Year Member



Hi Matt,

Tried the strtotime solution:

$event = "02-28-2009";

$registration_date = "03-28-2008";

$time_difference = strtotime($event) - strtotime($registration_date); // Get the number of seconds between the two dates.

$weeks_between = ceil($time_difference / (60 * 60 * 24 * 7));

echo $weeks_between;

The output is '-52'?

bubbasheeko

4:21 am on May 7, 2008 (gmt 0)

10+ Year Member



Hi Matt,

I found this and it seems to work like a charm:

/**
* array timeDiff(int $t1, int $t2)
* $t1 and $t2 must be UNIX timestamp integers, order does not matter
* returns array broken down into years/months/weeks/etc.
*/
function timeDiff($t1, $t2)
{
if($t1 > $t2)
{
$time1 = $t2;
$time2 = $t1;
}
else
{
$time1 = $t1;
$time2 = $t2;
}
$diff = array(

'weeks' => 0,

);

foreach(array('weeks')
as $unit)
{
while(TRUE)
{
$next = strtotime("+1 $unit", $time1);
if($next < $time2)
{
$time1 = $next;
$diff[$unit]++;
}
else
{
break;
}
}
}
return($diff);
}

$start = strtotime('2008-05-06');
$end = strtotime('2009-05-06');
$diff = timeDiff($start, $end);
foreach($diff as $unit => $value)
{
echo " $value $unit";
}