I'm using sfWidgetFormI18nDateTime in a form, and I have to add more years to it (back in time). For example 2007, 2006, 2005 etc.
sfWidgetFormI18nDate widget does support the "years" attribute, but DateTime doesn't.
Here's my code:
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array('culture' => 'hu'));
$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('required' => false));
I need something like this:
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array('culture' => 'hu', 'years' => metWidgetTools::getYears()));
$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('required' => false, 'years' => metWidgetTools::getYears()));
Is there a way that I can go around this?
Your widget schema should looks something like this
$years = range(2000, date('Y')); // from to 2000 to current year
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array(
'culture' => 'hu',
'date' => array(
'years' => array_combine($years, $years)
)
));
Related
I'm trying to add a year feature in my code.
Here, I push the years in an array, which can be selected after, using a select
$years = 2020;
$years_array = array();
// up-to-date years in array, starting from year 2020
while ($years <= date('Y'))
{
$to_push = array(strval($years)=>$years);
array_push($years_array, $to_push);
$years++;
}
$choices = array_values($years_array);
Then, I add them to the builder (I also show you the months, this has been developed by someone before me)
->add('mois', ChoiceType::class, [
'choices' => [
'Janvier' => 1,
'Février' => 2,
'Mars' => 3,
'Avril' => 4,
'Mai' => 5,
'Juin' => 6,
'Juillet' => 7,
'Aout' => 8,
'Septembre' => 9,
'Octobre' => 10,
'Novembre' => 11,
'Décembre' => 12
],
'data' => date('n')
])
->add('annee', ChoiceType::class, [
'choices' => $choices,
'data' => date('Y')
It works fine, I get the year afterwards, and the text shows up in the Select tag.
But I see the indexes of the array, as shown in this picture, which looks terrible.
How could I take these off?
You're nesting the associative arrays into an indexed array. You should just assign directly to indexes of $years_array.
for ($year = $years; $year < date('Y'); $year++) {
$years_array[$year] = $year;
}
You don't need to use array_values() afterward.
Hi i am using MongoDB as database in my cakephp application. and i am using this plugin for it. i want to search record(document) from my user collection. i am using this snippet
$userDetails = $this->User->find('all',array(
'conditions' => array(
'userId' => 18
)
));
it's working for me.
Now i wants to fetch the record by date like all user who was created in January Month of 2016. so for that i have to use similar to DATE_FORMAT function of MySQL and it might something like
$month = date('m');
$userDetails = $this->User->find('all',array(
'conditions' => array(
'userId' => 18,
'DATE_FORMAT(User.created_at,"%m") =' => $month
),
'fields' => array()
));
but how to use this type of Functionality with mongoDB can any one help me please?
thanks.
Try this :
var start = new Date(2016, 01, 01);
var end = new Date(2016, 01, 31);
db.User.find({userId: 18, created_at: {$gte: start, $lt: end}});
Source : here
I have a multidimensional array like this:
$array = array(
0 => array(
'name' => 'first element',
'start' => '30/04/2015',
'end' => '30/06/2015'
),
1 => array(
'name' => 'second element',
'start' => '01/07/2015',
'end' => '30/09/2015'
),
2 => array(
'name' => 'fourth element',
'start' => '01/10/2015',
'end' => '15/12/2015'
)
);
I need to select one array subarray (item) based on the today date.
today date must be between start date and end date keys.
In the end I would like to have this:
$selected_subarray = array (
'name' => 'first element',
'start' => '30/04/2015',
'end' => '30/06/2015'
)
I use to check between two dates like this:
function check_if_in_range($start_date, $end_date, $today_date)
{
$start_d = strtotime($start_date);
$end_d = strtotime($end_date);
$today_d = strtotime($today_date);
return (($today_d >= $start_d) && ($today_d <= $end_d));
}
I tryed to follow suggestions from this question How to search by key=>value in a multidimensional array in PHP
but if I'm able to filter for a key = value, I'm not able to do the same using the "check_if_in_range" function.
You do know that 30/06/2015 is invalid date, and strtotime() will return false? See here. Format mm/dd/yyyy is an American month, day and year. So your format is non-standard.
Best way is to convert it to standard format, and then use strtotime()example or just use DateTime::createFromFormat()example.
After you learn how formating and converting dates works, then you can just do simple foreach loop, and break on first found result. Here is a little demo.
Try something like the following
foreach($array as $key => $value) {
if(check_in_range($value['start'], $value['end'], $today_date)) {
$selected_subarray = $value;
}
}
I'm trying to let users post from the front end into a nested ACF repeater field.
I've got the sub fields in the first repeater posting fine. But, I can't quite figure out how to get the nested repeater working properly.
Here's the code I have so far:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '09:00', // would be dynamic
'end-time' => '17:00' // would be dynamic
)
);
update_field($event_field_key, $events, $post_id);
I'm not sure if I can just nest another array in there, or if I need to do something else.
[UPDATE]
I've just done this and it does input into the first row:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '9:00',
'end-time' => ' 17:00'
)
);
update_field($event_field_key, $events, $post_id);
However, this code puts row 1 values both as 9 and row 2 values as 1.
So it looks like:
Row 1: start time: 9, end time: 9
Row 2: start time: 1, end time: 1
I can't seem to find any documentation on this, but it looks like it's possible, just a case of figuring out the syntax.
The fix was an array of arrays:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
array(
'start-time' => '09:00',
'end-time' => '17:00'
),
array(
'start-time' => '10:00',
'end-time' => '16:00'
)
)
);
update_field($event_field_key, $events, $post_id);
I have a set of selects for a date of birth:
<?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'minYear' => date('Y') - 100
, 'maxYear' => date('Y') - 13)); ?>
and want to set the defaults to be the words "DAY MONTH YEAR" in the selects.
I have managed to do this with the gender with:
<?php echo $this->Form->input('Profile.gender', array('label' => 'Gender', 'type' => 'select',
'options' => array('Male'=>'Male','Female'=>'Female'),'empty'=>'Select Sex')); ?>
but I don't see how to do this with the automagic date input...
Can anyone help? Thanks
Simply add:
'selected'=>date('Y-m-d')
to your array of options.
That example will show the current date. If you need to have a static date, replace it as required. eg:
'selected'=>'2011-12-10'
Obviously for the date and time, use:
'selected'=>date('Y-m-d H:i:s')
or
'selected'=>'2011-12-10 11:13:45'
This way works:
<?php
echo $this->Form->input(
'Profile.dob',
array(
'label' => 'Date of Birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 100,
'maxYear' => date('Y') - 13,
'empty' => array(
'day' => 'DAY',
'month' => 'MONTH',
'year' => 'YEAR'
)
)
);
?>
If you don't mind 2 more lines, you can try doing this?
<?php
echo $this->Form->year('Profile.dob', date('Y') - 100, date('Y') - 13, array('empty' => "YEAR"));
echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
echo $this->Form->day('Profile.dob', array('empty' => 'DAY'));
?>
I implemented it like this in cakephp 2.0 above
echo $this->Form->dateTime('dob', 'DMY','', array(
'value'=>'1987-02-12',
'empty'=>false,
'label'=>'Date Of Birth',
'minYear'=>date('Y')-60,
'maxYear'=>date('Y')-15)
);
'value' attribute has been added after 2.0 api of cakephp and 'selected' is remove.
Cakephp manual says :The $selected parameter was removed from several methods in FormHelper. All methods now support a $attributes['value'] key now which should be used in place of $selected. This change simplifies the FormHelper methods, reducing the number of arguments, and reduces the duplication that $selected created. The effected methods are:
FormHelper::select()
FormHelper::dateTime()
FormHelper::year()
FormHelper::month()
FormHelper::day()
FormHelper::hour()
FormHelper::minute()
FormHelper::meridian()
Also, are you really sure about what you really mean?
You seem to be confusing default value and empty value. If you set default value as DAY MONTH YEAR using 'selected', your code won't work because DAY MONTH YEAR isn't a valid date.
Using
'empty' => array(
'day' => 'DAY',
'month' =>'MONTH',
'year' =>'YEAR'
);
looks like what you're looking for, inviting the user to enter a date.
Cakephp set selected to the empty value if the corresponding date request data is null, therefore simply set it to null prior to echoing the date input fields:
$this->request->data['Profile']['dob'] = null;
echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
// do the same for year and day
the empty value (in your case "DATE", "MONTH", "YEAR" fields) will be pre-selected in the input form
This is what currently works for me in cakephp 2.5:
echo $this->Form->input('fecha_pos_fijacion', array(
'label' => 'Fecha de fijación',
'dateFormat' => 'DMY',
'minYear' => date('Y'),
'maxYear' => date('Y')+5,
'orderYear' => 'asc',
'selected' => date('Y-m-1')
)
);
This is a bit more elaborated, it'd give you a default value set to the 1st day of current month. Possible values for year are between current and 5 years ahead, displayed in ascending order.
Even more complete:
echo $this->Form->input('fecha_transporte', array(
'label' => '',
'dateFormat' => 'DMY',
'minYear' => date('Y'),
'maxYear' => date('Y')+5,
'orderYear' => 'asc',
'selected' => date('Y-m-1', strtotime("+30 days"))
)
);
Here default is 1st day of next month