3

I have a number of days to a date in the future but would like to know how many weeks and days it is. Also, noting that if its less than a week, then it simply returns the same number.

Is this possible?

e.g. 17 days would be 2 weeks and 3 days

e.g. 4 days would be 4 days

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • 6
    The modulo operator is not sufficient? How? `$x % 7` gives you the number of days which didn't fit into a whole week, and you can work out the number of weeks from there trivially. See also: The Fine Manual, over there: http://php.net/manual/en/language.operators.arithmetic.php – Piskvor left the building Jan 02 '12 at 15:47
  • 1
    +1 for @Piskvor's suggestion, but if you ever need more than weeks, you might look into PHP's [DateInterval](http://php.net/manual/en/class.dateinterval.php) class. – Jimmy Sawczuk Jan 02 '12 at 15:49
  • Piskvor's right. Get the weeks by `round($x / 7)`, and then the days by `$x % 7` – Nonym Jan 02 '12 at 15:50
  • @Nonym: `$weeks = round($x/7); $days = ($x%7)` won't do *quite* what you'd expect for `$x==13`. PHP has three different rounding functions for a good reason. – Piskvor left the building Jan 02 '12 at 15:54
  • You're right, piskvor, but perhaps `$weeks = floor($x/7); $days = ($x%7)` would work, will it? – Nonym Jan 02 '12 at 16:12
  • @Nonym: Quite so, as evidenced by the twenty-odd answers below ;) – Piskvor left the building Jan 02 '12 at 16:29

7 Answers7

9

I would try something like this:

$days = 17;
$weeks = floor($days / 7);
$dayRemainder = $days % 7;
echo $days.'<br/>'.$weeks.'<br/>'.$dayRemainder;//add whatever logic you need here to get the display the way you want it.
john
  • 1,322
  • 11
  • 22
2
$weeks = intval($days / 7);
$days = $days % 7;

if($weeks)
{
    printf("%d weeks", $weeks);
}
if($days)
{
    if($weeks)
    {
        printf(" and ");
    }
    printf("%d days", $days);
}
1

Something along the lines of this should work

function getnumweeks(d) {
   totalDays = d;
   numWeeks = floor(d/7);
   if numWeeks != 0 {
      extraDays = totalDays % 7;
      return array(extraDays, numWeeks);
   } else {
      return array(totalDays, 0)
   }
}

Then you can call and use it as such:

ans = getnumweeks(17)

ans[0] <- Contains number of days
ans[1] <- Contains Number of Weeks
Drahkar
  • 1,694
  • 13
  • 17
0

As the Piskvor mentioned, you should use the modulo operator:

$weeks = $days/7;
$daysleft = $days%7;
MrGlass
  • 9,094
  • 17
  • 64
  • 89
0

Let's say x is number of days, W is output value of weeks and D is output value of days remaining.

First do integer division

W = x / 7;

Then you take remainder: D = x % 7;

rkosegi
  • 14,165
  • 5
  • 50
  • 83
0
$num_days = $databack30[days_to_next_event]; 
$weeks = floor($num_days/7); 
$days = $num_days % 7;

if($weeks>'0'){ $whenitis = ' in '.$weeks.' weeks and '.$days.' days'; }

else { $whenitis = ' in '.$days.' days'; }
StudioTime
  • 22,603
  • 38
  • 120
  • 207
-1

I would suggest you to re-use this powerful function datediff:

http://www.addedbytes.com/lab/php-datediff-function/

as suggested in php weeks between 2 dates

or taking inspiration from that code.

Community
  • 1
  • 1
simonecampora
  • 397
  • 2
  • 8