根据开始日期和持续时间计算结束日期

时间:2016-05-26 08:15:00

标签: php jquery ajax date

我有Jquery日期选择器来选择开始日期和下拉列表以选择周数。

我正在使用以下代码来获取结束日期结果,但它不起作用:

    week_number_id.on('change', function(e) {
    var selectvalue = $(this).val();
    //Display 'loading' status in the target select list
    date_result_id.html('<i class="uk-icon-spinner uk-icon-spin"></i>');
    if (selectvalue == '') 
    {
        date_result_id.html(initial_date_result_html);
    } 
    else 
    {
        //Make AJAX request, using the selected value as the GET
        $.ajax({
            url: 'index.php',
            data:'option=com_mycom&task=getmydateHTML&dvalue='+selectvalue,
            success: function(output) {
                date_result_id.html(output);
                updateSelect(date_result_id.val());
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status +  ' ' + thrownError);
            }
        });
    }
});

和PHP代码:

 public function getmydateHTML() {
     $jinput = JFactory::getApplication()->input;
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);         
     $dt = $jinput->get ('dvalue');
     $choosendate = $jinput->get ('start_date');       
     $newdate = strtotime("+". $dt . "week", $choosendate);
     echo   date('M d, Y', $newdate);     
    exit; // this will stop Joomla processing, and not output template modules etc.
}     

结果计算日期从1970年1月1日开始,周数正在增加,但代码无法获取开始日期

1 个答案:

答案 0 :(得分:1)

您需要将$choosendate转换为时间戳。您需要使用有效格式从日期创建日期。

请参阅示例: https://3v4l.org/SYltO

$week = "+1 weeks"; //"+". $dt . "week"
$str = 'jan 02, 2016';//$jinput->get('start_date')
$date = date_create($str);
$choosendate = date_format($date, "m/d/Y");    
$newdate = strtotime($week, strtotime($choosendate));
echo   date('M d, Y', $newdate); 
相关问题