Bill McCormick schreef:
> I have a duration $dur = $dt_end - $dt_start and I would like to know how
> many total weeks and/or days are in this span. How can that be easily
> accomplished?
> 
> I pretty sure I'll run into leap year issues if I say ...
> 
>       $weeks = ($dur->months * 4) + 1

Not only leap year issues; two months is closer to 9 weeks than to 8
weeks.

> I feel like I'm missing the blindingly obvious here.

Not really. We are currently debating the meaning of the subtraction of
two datetimes; at the moment you cannot get a span expressed only in
days.

In a next version of DateTime, you will probably be able to say
something like:

    $dur = $dt_end->subtract_datetime( datetime => $dt_start,
                                       units => [ 'days' ] );
    $weeks = $dur->weeks

(I use "days" instead of weeks here. Durations are not expressed in
weeks, because a week is always an exact number of days, and can
therefore be calculated from the number of days. But the exact syntax of
this function isn't fixed yet.)

For now, this would probably the best solution:

    $days = int($dt_end->jd) - int($dt_start->jd);
    $week = int $days/7;

Eugene

Reply via email to