[英]Php - weeks/days from days
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? 这可能吗?
eg 17 days would be 2 weeks and 3 days 例如17天将是2周和3天
eg 4 days would be 4 days 例如4天将是4天
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.
$weeks = intval($days / 7);
$days = $days % 7;
if($weeks)
{
printf("%d weeks", $weeks);
}
if($days)
{
if($weeks)
{
printf(" and ");
}
printf("%d days", $days);
}
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
As the Piskvor mentioned, you should use the modulo operator: 正如Piskvor所提到的,你应该使用模运算符:
$weeks = $days/7;
$daysleft = $days%7;
Let's say x is number of days, W is output value of weeks and D is output value of days remaining. 假设x是天数,W是周的输出值,D是剩余天数的输出值。
First do integer division 先做整数除法
W = x / 7; W = x / 7;
Then you take remainder: D = x % 7; 然后取余数:D = x%7;
$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'; }
I would suggest you to re-use this powerful function datediff: 我建议你重新使用这个强大的功能dateiff:
http://www.addedbytes.com/lab/php-datediff-function/ http://www.addedbytes.com/lab/php-datediff-function/
as suggested in php weeks between 2 dates 正如两个日期之间的php周所示
or taking inspiration from that code. 或从该代码中获取灵感。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.