[英]calculating number of months and generate tickets on each month
我正在尝试根据用户输入的开始日期和结束日期自动生成支持请求。
首先,我将计算两个日期之间的月数,然后我将在每个月提出支持请求,直到达到结束日期为止。
我正在尝试这样
if($frequency=='Monthly') {
$ts1 = strtotime($sdate);
$ts2 = strtotime($edate);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
for($i=1; $i<=$diff; $i++)
{
$time = strtotime($sdate);
$final = date("Y-m-d", strtotime("+1 month", $time));
$sql = mysqli_query($con, "SUPPORT REQUEST QUERY");
}
}
例如:开始日期为2017-06-19,结束日期为2017-08-21,则支持请求应在2017-06-19、2017-07-19和2017-08-19上生成3次
请建议如何前进
我认为没有必要计算月份数,除非您要将其存储在数据库中或在某个地方使用它。
您可以按照以下代码从用户那里获取开始日期和结束日期,并以所需的频率提高门票。
$startdate = "2017-06-19";//here is start date
$enddate = "2017-08-21";//end date
$tempdate = $startdate;//temporary storing start date
//comparing tempdate is not greater than end date
while(strtotime($tempdate)<=strtotime($enddate))
{
//this is my sample code. You can replace with yours.
echo "Raised ticket on ".$tempdate."<br/>";
$tempdate = date('Y-m-d', strtotime("+1 month", strtotime($tempdate)));
}
OUTPUT
Raised ticket on 2017-06-19
Raised ticket on 2017-07-19
Raised ticket on 2017-08-19
尝试这个
$sdate = "2017-06-19";
$edate = "2017-08-18";
$d1 = new DateTime($sdate);
$d2 = new DateTime($edate);
$diff = $d1->diff($d2)->m;
$Tickets = array($sdate);
$time = strtotime("+1 month", strtotime($sdate));
for($i=1; $i<=$diff; $i++)
{
$Tickets[] = date("Y-m-d", $time);
$time = strtotime("+1 month", $time);
//$sql = mysqli_query($con, "SUPPORT REQUEST QUERY");
}
var_dump($Tickets);
现在,您将门票放入$ Tickets数组中
编辑; 注意,如果结束日期少于开始日期,则所使用的差异计算将无法正常工作。 更改为DateTime。
演示: https : //3v4l.org/8UVRM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.