从持续时间计算结束日期


calculate end date from duration

我有一个duration下拉菜单选项'年度/季度/每月'和start date文本框与日期拾取器。还有一个文本框end_date.

根据持续时间,当选择持续时间和开始日期时,我需要结束日期。

。:开始日期是23-09-2015,选择的持续时间是3 months,所以,我需要显示23-12-2015,但我不知道如何做到这一点

PHP(5.3+)有一个简洁的类来帮助为日期添加持续时间,它被称为DateInterval。(它还有许多其他很好的间隔特性)

$date = new DateTime('2015-10-02');
$date->add(new DateInterval('P3M'));
echo $date->format('Y-m-d'); // 2016-01-02

在上面的例子中,我给我的日期加了3个月。你可以做其他更复杂的事情,比如P4Y1M2D,它是2天,1个月和4年....

查看php文档获取更多信息:为日期添加持续时间或间隔

试试这个。这是一个例子。我希望这对你有帮助。

<?php
  $date = date("Y-m-d");
  $date = strtotime(date("Y-m-d", strtotime($date)) . " +3 month");
  echo date("Y-m-d",$date);
?>

如果你需要添加月份和日期,这样做

$date = strtotime(date("Y-m-d", strtotime($date)) . " +3 month 2 day");

use

 $date = date_create('2015-09-30');
date_add($date, date_interval_create_from_date_string('3 Months'));
echo date_format($date, 'Y-m-d');

请检查

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

    <script>
    $(document).ready(function(){
         $("#start_date").removeClass('hasDatepicker').datepicker(
            { 
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy-mm-dd',
                onSelect: function(selectedDate)
                {
                    var d = new Date( selectedDate );
                    if($('.date_range').val() == 'Yearly')
                    {
                        d.setFullYear( d.getFullYear( ) + 1 );                  
                    }
                    if($('.date_range').val() == 'Quarterly')
                    {
                        d.setMonth( d.getMonth( ) + 3 );                
                    }
                    if($('.date_range').val() == 'Monthly')
                    {
                        d.setMonth( d.getMonth( ) + 1 );                
                    }
                    year = d.getFullYear();             
                    month = ("0" + (d.getMonth() + 1)).slice(-2);
                    date = ("0" + d.getDate()).slice(-2);
                    end_date = year +'-'+month+'-'+date;
                    $('#end_date').val(end_date);
                }
            });
    });
    </script>
    <select name="date_range" class="date_range">
      <option value="None">Please select a date range</option>
      <option value="Yearly">Yearly</option>
      <option value="Quarterly">Quarterly</option>
      <option value="Monthly">Monthly</option>
    </select>
    <p>Start Date</p>
    <input type="text" name="start_date" id="start_date" />
    <p>End Date</p>
    <input type="text" name="end_date" id="end_date" />