开发者

What is the best way to limit a date range in a CakePHP view?

开发者 https://www.devze.com 2023-01-13 16:32 出处:网络
I have a date field which the users of the system can edit, but there are limitations on what they may be able to select.Right now it is simple and just limited to \"n\" number of days into the future

I have a date field which the users of the system can edit, but there are limitations on what they may be able to select. Right now it is simple and just limited to "n" number of days into the future. But I could see it getting more sophisticated with only allowing weekdays, etc. I can 开发者_开发百科write the select elements manually, but I'm curious if there are any helpers that have started tackling this.


There is nothing that I have found. But this should be simple enough to create:

// populate with the weekdays you want (0=Sunday, 1=Monday, etc.)
$weekdays = array(1,2,3,4,5);

$dates = array();
$today = strtotime(date("Y-m-d"));
$end_date = strtotime("+6 months");
while($today < $end_date) {
    $weekday = date("w", $today);
    if (in_array($weekday, $weekdays)) {
        array_push($dates, date("Y-m-d", $today));
    }
    $today += 86400;
}

$this->set('dates', $dates)

Then in the view:

$this->Form->input('my_date', array('type' => 'select', 'options' => $dates));
0

精彩评论

暂无评论...
验证码 换一张
取 消