2017-02-06 99 views
-1

我想在cakephp中編寫一個代碼來自動設置從循環中取得的10個特定月份的月份的第一個和最後一個日期。以下是表單截圖。設置10個月的第一個和最後一個日期

我需要的是:如果月份是從6月1日起應該是2016年6月1日,並且日期將默認爲30/06/2016,允許用戶更改日期。其他月份和年份應該禁用。同樣,如果月份是七月,它應該是2017年7月1日至2016年7月31日。

這應該從當前月份開始持續10個月。下面是腳本

<script> 
    //jQuery.noConflict(); 
    jQuery(document).ready(function() { 

    $(".last_from_date").datepicker({ 
     dateFormat: "dd-mm-yy", 
     changeMonth: false, 
     maxDate: '0M', 
     changeYear: true, 
     yearRange: '1900:2020' 
    }); 
    $(".last_to_date").datepicker({ 
     dateFormat: "dd-mm-yy", 
     changeMonth: true, 
     maxDate: '0M', 
     changeYear: true, 
     yearRange: '1900:2020' 
    }); 

    $('.abc').click(function() { 
     alert($(this).attr("id")); 
     var id = $(this).attr("id"); 
     var a = id.split("-"); 
     var year = $.trim(a[0]); 
     alert(year); 
     var month = $.trim(a[1]); 
     alert(month); 

     var dat = new Date('1 ' + month + year); 
     alert(dat.getMonth() + 1); 
     //   alert(LastDayOfMonth(year,month)); 
     //   alert(x); 
     //   $("#datepicker").datepicker("setMonth", newDate); 


     var currentTime = new Date(dat); 
     // First Date Of the month 
     var startDateFrom = new Date(currentTime.getFullYear(), currentTime.getMonth(), 1); 
     //// Last Date Of the Month 
     var startDateTo = new Date(currentTime.getFullYear(), currentTime.getMonth() + 1, 0); 
     $("#last_from_date_" + id).datepicker({ 

     dateFormat: 'dd.mm.yy', 
     minDate: startDateFrom, 
     maxDate: startDateTo 
     }); 
    }); 

    }); 
</script> 

與記錄

enter image description here

每一行都有一個類「ABC」和從並箱子是datepickers圖片形式。

錯誤:我無法爲這些日期設置日期。

回答

0

我不知道如何寫在JavaScript,但正如你所說,這是一個PHP CakePHP的應用程序在generall它是這樣的,如果你知道年份和月份:

$firstdateofMonth = date("Y-m-d", strtotime("$year-$month-1"));

$lastdateofMonth = date("Y-m-t", strtotime("$year-$month-1"));

( 「t」總是給定月份的最後一天)。

相關問題