Yii2 Boostrap period picker - yii2

I'm using Yii2 and I would like to have the following things:
A dropdown with "ranges" as items (week, full week, week-end, etc.) (done)
A RangePicker that based on the chosen range will:
allow only some days as the startDate (ex: week = mondays; week-end = saturdays)
will automatically select the end date based on the start date (ex: week: monday to friday; full week: monday to sunday; week-end: saturday to sunday, etc.)
For the dropdown, it's fine. But for the DateRangePicker, I started using Krajee DateRangePicker and
I do not find a callback for "onStartDateSelected" (I only saw a callback that is called when the range is selected, but not for individual start date or end date)
I do not see a "setStopDate" function
I do not see how to dynamically change the available days of weeks
I see that we can have a set of predefined periods like "last 7 days", "this month", etc. But this is not what I want. I want the user be able to select the start date but automatically calculate the end date based on what the user has set.
(I'm new in web dev, so please if you find anything that I could improve in my question, don't be afraid to share it)

You can use the following to accomplish the task:
Use the following library:
use kartik\date\DatePicker;
Use the following code in view:
<?php
$layout3 = '<span class="input-group-addon">From Date</span>
{input1}
<span class="input-group-addon">To Date</span>
{input2}
<span class="input-group-addon kv-date-remove">
<i class="glyphicon glyphicon-remove"></i>
</span>';
$previousDay = strtotime('-7 day', strtotime(date('d-M-Y'))); //Set as per your requirement[![enter image description here][1]][1]
echo DatePicker::widget([
'type' => DatePicker::TYPE_RANGE,
'name' => 'startDate',
'value' => date('d-M-Y', $previousDay),
'name2' => 'endDate',
'value2' => date('d-M-Y'),
'separator' => '<i class="glyphicon glyphicon-resize-horizontal"></i>',
'layout' => $layout3,
'pluginOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);
?>

Related

Yii2 Kartik DateTimePicker start date from far future

I'd like to ask you for support. The yii2 Kartik datetimepicker extension is giving strange and random start date. After clicking the calendar the start date might be year 4678 or year 3599. Imagine scrolling this back to 2017 :) Have you ever faced similar issues. I have checked the forum and I was not able to find the answer.
Here is my code.
<?php
// Usage with model (with no default initial value)
echo $form->field($date, 'start_time')->widget(
DateTimePicker::className(),
[
'size'=>'md',
'type' => DateTimePicker::TYPE_COMPONENT_APPEND,
'convertFormat' => true,
'pluginOptions' => [
'todayHighlight' => true,
'todayBtn' => true,
'format' => 'dd-M-yyyy H:i',
'autoclose' => true,
'startDate'=> date('d-m-Y H:i', strtotime(time())),
],
])->label('Start Date');
?>
Here is the screen shot of the datetimepicker: https://ibb.co/fu5WFw
The second parameter of date() is a timestamp, so remove strtotime():
'startDate'=> date('d-m-Y H:i',time()),

2amigos/yii2-date-picker-widget year validation

I am using 2amigos/yii2-date-picker-widget.After selecting the date, user can edit the year which can be invalid like 10/30/0233. (m/d/Y)
I want to restrict user to enter invalid year, or to allow the date between specific year range only. without making the datefield readonly. How can I achieve this.
echo $form->field($model, 'date')->widget(DatePicker::className(), [
'addon' => '<i class="fa fa-calendar" aria-hidden="true"></i>',
'class' => 'form-control',
'clientOptions' => [
'autoclose' => true,
'format' => Yii::$app->params['dateFormat'],
],
]);
I have solved it using custom validator as follows
add rule for date as follows
['my_date', 'validYear'], // custom validator to check year range
Then define custom rule as follows
public function validYear($attribute, $params) {
$date = DateTime::createFromFormat("m/d/Y", $this->my_date);
$year = $date->format("Y");
$year_range = range(date('Y'), date('Y')+20, 1);
if(!empty($year) && !in_array($year, $year_range)){
$this->addError($attribute,"Please select valid date");
}
}
Though it works perfectly for me, If anyone has more appropriate solution, please suggest.

DateType and various formats

I have DateType. For example for saving foundation of some band to our database.
->add('founded', DateType::class, [
'label' => 'Founded',
'widget' => 'single_text',
'required' => false
])
There is unknown full date sometimes. There can be only a year and month known, or just a year without the day and month. But if I fill an incomplete date to this HTML5 input, nothing is saved and I have HTML5 error.
How is it possible to parse full date, year and month or just year with DateType and HTML5 input? Or is there any other alternative and clean solution?

Cakephp 3.0 I would like to include a Year input field with a drop down, but it is inputing as an array

I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs

Cakephp Conditions Between Times or if time column is not a time

I have a table I need to pull records from if the row is between certain times. But some of the times are UFN(until further notice). How can I get cakephp to ignore the 'end' time if the end time is a string and not a time? Would it be easier if I force the user to keep the 'end time' blank if they want to display UFN?
Thanks in advance
EDIT:
The project I'm working on is sensitive so I can't post any of the actual code, but here's an example.
$this->Event->find('all', array(
'conditions' => array(
'Event.active_start <' => date('Y-m-d H:i:s', strtotime($date . " +31 minutes")),
'Event.active_stop >' => $date,
'Event.id >' => $id,
),
'order' => 'Event.active_start',
'group' => 'Event.id'
)
);
Event.active_start is always a datetime, but Event.active_stop can be a datetime or a string, usually 'UFN'.
This query is not pulling any rows that are strings.
Make 'Event.active_stop' as DATETIME field, allow to be empty.
Before save check if datetime format, if not unset($string)
After find (callback) if active_stop is empty field, return string 'UFN'