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
Related
I need to query the date:- 2020-01-06 If this date ( 2020-01-06 ) exists in the _from field in the post meta table.
_form field is in timestamp format like 1578304800.
This is how I am trying to do.
$theBookingTime = strtotime( '2020-01-06' );
$events_query = new WP_Query(
array(
'post_type' => array('yith_booking', 'post'),
'meta_query' => array(
array( 'key' => '_from',
'value' => date('c', $theBookingTime),
'compare' => '=',
'type' => 'DATETIME',
)
)
));
ADDING FOR INFO :_
And if we convert the time stamp "1578304800" to date it get 2020-01-06 10:00:00
And i need to compare only the date with the given timestamp in table field ( _form )>
I need to find the 2020-01-06 ** Only the date part from the date and time in the table**>>
If this can be done with the custom mysql also? please suggest.
You're using 'value' => date('c', $theBookingTime),
The 'c' corresponds to the output format of your date. What you're looking for is 'value' => date('Y-m-d', $theBookingTime), where Y will output the year in 4 digits, m will output the month and d - the day.
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)
)
));
I'm trying to implement a date selector in Cake-1.3. The problem I'm having is that I've not been able to confine the year selector to the desired set of years, which is 2012 through the current year, whatever that might be. I've tried 3 or 4 ways of coding this, based on my own thoughts and on examples from the cakephp site, Stack Overflow, and at least one other site. No matter which approach I take, the years in the selection list range from 1994 to 2034.
Here is the current version of the code for this:
echo "<div class='date'>" . $this->Form->input('start_date',
array('type'=>'date',
'default'=>array('month'=>$yesterday['month'],
'day'=>$yesterday['mday'],
'year' => $today['year'],
array('dateFormat' => 'MDY', 'minYear' => 2012, 'maxYear' => $yesterday['year'],
selected)
)
)
) . "</div>";
Any suggestions would be highly appreciated.
Your input options should all be in a single unnested array.
echo "<div class='date'>" . $this->Form->input('start_date',
array('type'=>'date',
'default'=>array('month'=>$yesterday['month'],
'day'=>$yesterday['mday'],
'year' => $today['year']),
'dateFormat' => 'MDY',
'minYear' => 2012,
'maxYear' => $yesterday['year']
)
)
. "</div>";
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 am trying to sort an array in PHP by date and time which is in ISO 8601 format. I am still trying to grasp PHP and have tried many of the solutions on stack overflow and I am just not able to nail down the right function. Hopefully this is an easy answer and it will be helpful to others.
FYI, this array was generated by the Citrix API for GoToMeeting. I would like to sort the array based on startTime in the soonest time first in the list.
Here is what the array looks like using var_export with two results presented:
array (
0 => stdClass::__set_state(
array(
'createTime' => '2012-07-03T19:36:58.+0000',
'status' => 'INACTIVE',
'subject' => 'Client 1',
'startTime' => '2012-07-10T14:00:00.+0000',
'conferenceCallInfo' => 'United States: xxxxx Access Code: xxxxx',
'passwordRequired' => 'false',
'meetingType' => 'Scheduled',
'maxParticipants' => 26,
'endTime' => '2012-07-10T15:00:00.+0000',
'uniqueMeetingId' => 12345678,
'meetingid' => 123456789,
)
),
1 => stdClass::__set_state(
array(
'createTime' => '2012-07-02T21:57:48.+0000',
'status' => 'INACTIVE',
'subject' => 'Client 2',
'startTime' => '2012-07-06T19:00:00.+0000',
'conferenceCallInfo' => 'United States: xxxxx Access Code: xxxxx',
'passwordRequired' => 'false',
'meetingType' => 'Scheduled',
'maxParticipants' => 26,
'endTime' => '2012-07-06T20:00:00.+0000',
'uniqueMeetingId' => 12345678,
'meetingid' => 123456789,
)
),
)
My goal is to then output the array into html div's using a foreach loop, this code is complete and works well but my sort is off :-)
Thank you in advance for any help!
Steve
You can implement any sorting technique you can think of if you wrap it in a callback and use usort() docs here
inside your callback, you can use strtotime or similar, and do simple int comparisons.
$myDateSort = function($obj1, $obj2) {
$date1 = strtotime($obj1->startTime);
$date2 = strtotime($obj2->startTime);
return $date1 - $date2; // if date1 is earlier, this will be negative
}
usort($myArray, $myDateSort);