【问题标题】:How to divided number based on date using php?如何使用php根据日期划分数字?
【发布时间】:2017-01-17 15:55:48
【问题描述】:

我有 2 个变量叫做

$joined_date = 2016-09-18

$monthly_bill = 1200

现在我想用每月账单显示从加入日期到今天的所有月份。例如,循环应该显示如下结果:

2016-09-18        480 // Joined 18th and month end 30th = 12 days * (1200 / 30) 
2016-10-01        1200 
2016-11-01        1200 
2016-12-01        1200 
2017-01-01        1200 

如何使用 php 循环显示这个?

【问题讨论】:

  • 有些事情没有意义:你说每月账单是 1200,但你的例子说是 1000。它是什么?此外,这不是“为我编写代码”网站。请展示您的尝试。
  • 哦,对不起,关于它。应该是 1200
  • 我已经编辑了我的问题。
  • @CD001 我的问题有点不同。我想用全数或小数显示所有月份。

标签: php


【解决方案1】:
    //cal_days_in_month : Return the number of days in a month for a given year and calendar  

----------

<?php
$join_date="2016-09-18";
$begin = new DateTime($join_date); // value is : 2016-09-18
$monthly_bill = 1200;
$date=$begin->format('d');
$month=$begin->format('m');
$year=$begin->format("Y");
$ndays=cal_days_in_month(CAL_GREGORIAN,$month,$year);
if($date>1)
{
    $totaldays=$ndays-$date;
}
else{
    $totaldays=$ndays;
}
$paidbill=$totaldays * ($monthly_bill / $ndays);
echo $paidbill."<br/>";


?>

【讨论】:

  • 嗨,Devi,让我检查一下。
  • 不知何故,在我的实际 php 循环中,计算没有显示实际的每月账单。在我的循环中,我得到所有用户的月度账单,即:350、250、150、300、200 和 100。(6 个用户)。但结果不正确
  • 第一个用户是 140, 350, 350, 350, 350
  • 你能发布你的代码,即你是如何为每个用户循环的吗?
  • while循环有点长
猜你喜欢
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多