I have a table with records of users (name, forename, birthdate, etc.) and I want to find users by name or birthdate or every possible combination of this.
My code works well, but the birthdate is stored in the american format (YYYY-MM-dd), so I have to search after dates in this format to get the right record. That´s really annoying, because in Germany we use the european format (dd.MM.YYYY) and I want to search for the dates by our habitual format. Now I need help to give the input field the european format and get the right record.
I hope, you understand my issue. :)
Here is an extract of my Controller-Code:
$name = $this->request->data['name'];
$forename = $this->request->data['forename'];
$birthdate = $this->request->data['birthdate'];
if($name || $forename || $birthdate){
$query = $this->find()
->where([
'name LIKE' => '%'.$name.'%',
'forename LIKE' => '%'.$forename.'%',
'birthdate LIKE' => '%'.$birthdate.'%'
]);
$number = $query->count();
$this->set('users', $this->paginate($query));
$this->set('_serialize', ['users']);
}
And here is the related part of the code from my view:
foreach($users as $user):
h($user->name)
h($user->forename)
h($user->birthdate)
endforeach;
Thank you very much.
Your birthdate is stored correctly.
set in bootstrap.php
/**
* Set the default locale. This controls how dates, number and currency is
* formatted and sets the default language to use for translations.
*/
ini_set('intl.default_locale', 'de_DE');
http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#setting-the-default-locale
view / search form
<?= $this->From->input('birthdate',['type' => 'text']); ?>
use js / jquery date picker
$('#datepicker').datepicker({ dateFormat: 'dd.mm.yy' });
Related
CakePHP 3.5.13. I am getting some records from MySQL which are stored as DATE fields. For example: 2018-07-30
I want Cake to output the dates in that format. However, it is converting them to British dd/mm/yyyy format. So the example above comes out as 30/07/2018 in my template files.
The data returned from the database looks like this:
'date' => object(Cake\I18n\FrozenDate) {
'time' => '2018-07-30T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
In my config/app.php the locale is set to en_GB:
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_GB'),
I assume this is the reason it's displaying all of the dates in this way.
All I am doing in the template is displaying the data returned from the database - no conversion is occurring within my code:
// In a controller
$RevisionSubstances = TableRegistry::get('RevisionSubstances');
$revision_comments = $RevisionSubstances->find('all')->toArray();
debug($revision_comments); // format is '2018-07-30T00:00:00+00:00'
// In the template
echo $revision_comments['date']; // format is '30/07/2018'
How do I get the dates to be output in yyyy-mm-dd format?
I had a look at these resources but can't see the answer:
https://book.cakephp.org/3.0/en/core-libraries/time.html
https://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#parsing-localized-dates
I currently use GF for reservations 'Arrive' and 'Departure'. I would like the date displayed on the 'Departure' to always be one date ahead of the 'Arrival' date, regards of what date the guest picks. Then can pick a custom 'Departure' date, but as a default I'd like to show one date forward of the 'Arrival' date no matter what 'Arrival' date they choose.
This is possible with the gform_datepicker_options_pre_init JS filter. Example #3 is what you're after:
gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
if ( formId == 12 && fieldId == 8 ) {
optionsObj.minDate = 0;
optionsObj.onClose = function (dateText, inst) {
jQuery('#input_12_9').datepicker('option', 'minDate', dateText).datepicker('setDate', dateText);
};
}
return optionsObj;
});
If you're looking for a code-less solution, I've written a plugin that let's you do this in just a few clicks called GP Limit Dates.
Also, here is an article that addresses your specific need: How to restrict dates in second date field based on date selected in first date field with Gravity Forms
I'm relatively new to Drupal 7 and I'm trying to create a custom webform. My goal is to add a date (provided by the date module) field with out the day option. So it displays on month and year hiding the day option.
I have managed to achieve this but only by recreating the wholedate field as a custom field but I wanted to know if it was possible to customize the date field provided by the date module.
Below is a screen shot of my form:
How I create my custom date field:
function my_webform_form_alter(&$form, &$form_state) {
if (isset($form['#node']->webform) && $form['#node']->uuid == '00b20537-d5ce-45c2-af37-150c9e73b96d') {
//$form['submitted']['date']['#type'] = 'hidden';
$form['ggg'] = array(
'#type' => 'date_select',
'#title' => 'Date',
'#date_format' => 'm/Y',
'#default_value' => date('Y-m-d')
);
}
}
I have tried other methods on hiding the field components but nothing seem to work so far. I was wondering if I needed to implement a hook different from the alter hook (the one being used).
Any suggestions on how to achieve this?
A possible solution would be to transform the day field of the date component to a hidden field instead of the select field type. That can be achieved by adding a #process callback for that field and altering the data.
function YOURMODULE_form_alter(&$form, &$form_state, $form_id)
{
// Your logic here depending which form to alter
// ...
// Add #process for the component with key name 'date'
$form['submitted']['date']['#process'][] = 'YOURMODULE_process_date';
}
function YOURMODULE_process_date(&$element)
{
// change type to hidden
$element['day']['#type'] = 'hidden';
// set value to first day of the month
$element['day']['#value'] = '1';
return $element;
}
Webform now allows hiding the day, month or year of a date. See this issue for details.
How to update all fields affected in MongoDB using cakephp. Say I have queried the Start and End Time. I want to update all the fields affected BETWEEN those time of an specific user.
<?php
$stime = $this->data["User"]["sTime"]; //$stime = "2:29 PM";
$etime = $this->data["User"]["eTime"]; //$eTime = "3:40 PM";
$user = $this->data["User"]["affected_user"];
?>
All the fields within the start and end time will be affected. I would like to update a field called status and set it to "1". Thanks
You can use the updateAll() statement to update multiple fields like.
<?php
// first of all convert the start time and end time in proper date format the use the statement like bellow.
$this->ModelName->updateAll(array('status' => 1), array('time >=' => $stime, 'time <' => $etime));
?>
If you want to update multiple fields then you can specify like status in the same array. For more information checkout the updateAll() documentation on the cakephp site.
I am working in Yii framework. I am having Poll table with fields as-
-pollId
-pollQuestion
-Isactive
-publishDate
-isPublish
when new poll is created,that date get inserted into publishDate field.
e.g.2012-04-04 02:23:45 In this format entry get inserted.
Now i want to check weather this publishDate is smaller than today's date or current date. i.e.publishDate should not be greater than current date.
So how to check this in yii? Please help me
As per normal PHP. Assuming $model is the submitted form and you have assigned (after the form has been submitted) $model->attributes = $_POST['MyModel']
You can then use:
if ($model->publishDate < date('Y-m-d H:i:s')){
// it is smaller
}
Another thing you could look at is using Yii's model validation. You could store the created date (which would be todays date) and then compare that to the publishDate in the form submit:
$model->created = date("Y-m-d H:i:s");
if ($model->validate){
...
}
And in your Poll model:
array('publishDate ','compare','created','operator'=>'<', 'message'=>'Publish Date must be smaller than the current date'),