php - 计算月数并生成每个月的票证

标签 php

我正在尝试根据用户输入的开始日期和结束日期自动生成支持请求。

首先,我将计算两个日期之间的月数,然后我将每月提出支持请求,直到到达结束日期。

我正在尝试这样

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,那么支持请求应该生成3次,分别是2017-06-19、2017-07-19和2017- 08-19

请建议如何继续

最佳答案

我认为没有必要计算月数,除非你想将其存储在数据库中或在某个地方使用它。

您可以仅从用户处获取开始日期和结束日期,并按照此代码按照您想要的频率提出票证。

$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)));
}

输出

Raised ticket on 2017-06-19
Raised ticket on 2017-07-19
Raised ticket on 2017-08-19

关于php - 计算月数并生成每个月的票证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44537862/

相关文章:

php - 在 PHP Mailer 中允许不安全的连接

php - 自动刷新html页面中的php变量

php - 没有收到任何 Facebook 实时页面更新请求

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

PHP Curl 请求不适用于 easypaisa 支付网关发布请求

php - 在 MySQL 中获取下一个时间戳

php - Phalcon - 我如何使用 Phalcon 模型执行 SELECT IN 子查询?

php - 禁用 "magic quotes"后,为什么 PHP/WordPress 继续自动转义我的 POST 数据?

php - 来自 PHP 字符串的 JavaScript 数组

php - 保存并下载 PDF