从持续时间计算结束日期

时间:2015-09-30 08:55:26

标签: php jquery-ui date

我有一个duration下拉菜单,其中包含“年度/季度/月度”选项以及带有datepicker的start date文本框。还有另一个文本框end_date。

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

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

4 个答案:

答案 0 :(得分:0)

试试这个。这是一个例子。我希望这会有所帮助。

<?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");

答案 1 :(得分:0)

使用

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

答案 2 :(得分:0)

请检查

<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" />

答案 3 :(得分:0)

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 doc:DateInterval

有关教程,请访问:Add a duration or interval to a date