How can i save date array to database?
the code belove creates a form for date
$form['date_time'] = array(
'#title' => t('Date'),
'#type' => 'date',
'#description' => t('Please enter the date.'),
);
User then selects the month,day,year.
How can i convert it so that i can save it to database?
function repman_form_submit($form, &$form_state){
drupal_set_message($form_state['values']['date_time']);
I am just using the drupal_set_message to view the output. And i get array.
UPDATE:
Done it using this
'date_month'=> $form_state['values']['date_time']['month'],
'date_day' => $form_state['values']['date_time']['day'],
'date_year'=> $form_state['values']['date_time']['year'],
You want to convert the date to ATOM or a unix timestamp:
// Dates are stored in ATOM format. 1969-12-31T19:33:33-05:00
$value = date(DATE_ATOM, mktime(0, 0, 0, $m, $d, $y));
// Convert to unix time
$value = date('U', mktime(0, 0, 0, $m, $d, $y));
I believe sql datetime uses ATOM but you can also stor it as a unix time stamp if you want to just stor it as an integer value.
Related
How can I concatenate two fields inside a select box using cakephp 2.5 and Hash:combine ?
Today I have name:
$banks = Hash::combine($banks, '{n}.Bank.id', '{n}.Bank.name');
I need name and identifier, I tried this:
$banks = Hash::combine($banks, '{n}.Bank.id', '{n}.Bank.name {n}.Bank.identifier');
But it return NULL.
How can I have : name - identifier ?
Also try to concatenate the two fields at the model but could not add an hiffen between name and identifier.
You can provide array’s for both $keyPath and $valuePath. If you do this, the first value will be used as a format string, for values extracted by the other paths:
$result = Hash::combine(
$a,
'{n}.User.id',
array('%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.group_id'
);
/* $result now looks like:
Array
(
[1] => Array
(
[2] => mariano.iglesias: Mariano Iglesias
)
[2] => Array
(
[14] => phpnut: Larry E. Masters
)
)
*/
$result = Hash::combine(
$a,
array('%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.id'
);
/* $result now looks like:
Array
(
[mariano.iglesias: Mariano Iglesias] => 2
[phpnut: Larry E. Masters] => 14
)
*/
See CookBook > Hash for mode details.
I am trying to calculate distance between two geographical locations. one location is entered by user.
User entered location is calculated by model and is stored in the variable.
$ulatitude=$userLocation['Location']['latitude'];
$ulongitude=$userLocation['Location']['longitude'];
I have used below code to calculate the distance.
$this->User->virtualFields['lowestDistance'] = 'round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) ';
$order = "User.lowestDistance";
$options = array(
'fields' => '
User.*, Profile.*,
round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) AS lowestDistance,
',
'order' => array($order =>'asc'),
);
Above code throws error : unknown column $ulatitude.
Is there any other way of doing this ?
Single quotes are not interpreted
Consider:
<?php
$foo = "A VARIABLE";
echo 'foo is $foo' . "\n";
echo "foo is $foo" . "\n";
This would output:
-> php example.php
foo is $foo
foo is A VARIABLE
In the question everything is in single quotes, they are literal strings.
Fields should be an array
Also note that this:
'fields' => 'User.*, Profile.*, ...
Is normally an array, which makes more sense when there's multiple values:
'fields' => [
'User.*',
'Profile.*',
...
]
But since you're declaring all fields plus a virtual field, it'd be included in a find call anyway - just remove the fields from the find parameters:
$this->User->virtualFields['lowestDistance'] = "round(...";
$options = [
//'fields' => [], Not necessary
'order' => [$order =>'asc']
];
$withLowestDistance = $this->User->find('all', $options);
Below cakephp query returns an empty array result but if I use only $max_results it works.
I want to search for a record from two limits (0,10).
This doesn't work.
$from = 0;
$max_results = 10;
$this->Model->find('all',array(
'conditions'=>$condition,
"order"=>'Model.id DESC',
'limit'=>"$from,$max_results"
));
You need to use offset options-
$from = 0;
$max_results = 10;
$this->Model->find('all',array(
'conditions'=>$condition,
"order"=>'Model.id DESC',
'limit'=>"$max_results",
'offset' => "$from"
));
I am new to cakephp and i wish to do the following:
I have a dateTime object and i want to add and subtract 30 minutes to it.
Following is my code in controller :
$time = $this->request->data['Rideoffer']['DepartureTime'];
$date = new DateTime($time['hour'] . ':' . $time['min'] . ' ' . $time['meridian']);
$currentDate = strtotime($date['date']); // this line gives error
$futureDate = $currentDate+(60*30);
$formatDate = date("Y-m-d H:i", $futureDate);
when i debug $date i get the following result:
object(DateTime) {
date => '2013-03-08 05:54:00'
timezone_type => (int) 3
timezone => 'UTC'
}
i wish to extract date from this object. How do i do so?
solved it:
$currentDate = strtotime($date->format('Y-m-d H:i:s'));
but i still dont get it that why cant i use $date['date'], i mean $date like an array.
Hi all I have a dropdown box on a view. I have a find statement that returns a list of templates, when I debug the find, it prints out the correct list of template.id and template.name however when I submit the form it carries across the number it is in the list for example if I select the 5th template in a list it saves the template_id = 5 NOT the actual template id number.
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//conditions for template arrays
$conditions=
array('AND' => array(
array('Template.account_id' => $accountid),
array('Template.active' => 1)
));
//An array of all Templates belonging to the current User's Account
$templates=$this->Template->find('list', array(
'conditions' => $conditions));
when I debug $templates I get this print out
array(
(int) 1 => 'Consulting- Bus',
(int) 2 => 'Consulting- Pers',
(int) 7 => 'ClientEdge',
(int) 8 => '11111',
(int) 9 => 'testSUn',
(int) 10 => 'Test Bruce Review',
(int) 11 => 'Test Bruce 3 = Final'
when I select for example 'Test Bruce Review' and hit submit and debug the value it prints out '6', the 6th item in the dropdown box when I want it printing out 10 when I debug it.
here is the snippet from the form relating to this dropdown box
<tr><td><?php echo "Template: "?></td>
<td><?php echo $this->Form->input('template_id', array('label'=>false,'type'=>'select','options'=>$templates));?></td></tr>
how can I remedy this issue?
I had an issue with merging my arrays that caused the death of my template.id