【问题标题】:Calculate end date based on start date and duration根据开始日期和持续时间计算结束日期
【发布时间】:2016-05-26 08:14:59
【问题描述】:

我有 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 日开始,周数正确增加但代码无法获取开始日期

【问题讨论】:

标签: php jquery ajax date


【解决方案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); 

【讨论】:

  • 请检查您的日期是否正确??检查这个$choosendate,如果它不是保持日期标准,那么你需要先转换它。但这是完成工作的过程。
  • 那么如何转换start_date值呢?
  • 好吧,如果你能给我$choosendate的值,那么我可以给你方法。
  • May/02/2016 但更喜欢 2016 年 5 月 2 日
  • 现在它显示日期和计算,但从今天开始!如果我选择第 1 周,它显示 2016 年 6 月 2 日。不是从日期选择器 (start_date) 中选择的那个让我们假设我选择了 2016 年 1 月 1 日,所以结果应该是 2016 年 1 月 8 日。
猜你喜欢
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
相关资源
最近更新 更多