CakePHP: How to change default value of Date input - php

I am trying to make a date input that defaults to the value that is already in the database.
However, when I set the month, year, and date to the database values, the month, date, and year attributes are set on the select element but the page still displays the current date as the default values. When the form is submitted, today's date is stored in the database.
Heres the code:
$mail_date_time = \explode(" ",$campaign["MailedDate"]);
$mail_date = explode("-",$mail_date_time[0]);
echo $this->Form->create("Campaign");
echo $this->Form->input("MailedDate",array(
'month' =>strtotime($mail_date[1]),
'year' => strtotime($mail_date[0]),
'day' => strtotime($mail_date[2])
));
echo $this->Form->end("Submit");
$mailed_date turns out to be: [0]=2009 [1]=11 [2]=11
Does anyone know how to solve this?
Thanks!

The Cake form helper includes formatting of dates... i.e. 'dateFormat' => 'DMY' in your MailedDate input.
The value displayed by the input field would default to the value contained in $this->data and can be overwritten completely using 'default' => 'value', or prefilled using 'empty' => 'value'.

In order to precompile the datetime fields is necessary to use the 'default' keyword:
$mydate = '2015-09-10 06:40:00'
echo $this->Form->input('datetime', array(
'label' => 'Date 2',
'default' => $mydate
));

Related

MySQL query for dates with CakePHP

I'm trying to select the values that fall between 2 dates, so I'll have to use <= and >=, however, my query is seemingly behaving as just less than or greater than and not considering values equal to the date.
I'm using CakePHP which saves dates in "Y-m-d h:i:a" format. I wanted to find dates on given week intervals (starting on Sundays), so I used the following.
$start_date = date('Y/m/d', strtotime('last Sunday', strtotime($timestamp)));
$formatted_start_date = str_replace("/","-",$start_date);
I tried to do find $start_date formatted as "Y-m-d" but then it wouldn't find the correct date, so I switched it to the way it is and used str_replace to format it to using "-" instead of "/".
$date_query = $this->Order->query("select * from orders where id = '$id' and created => '$formatted_start_date' and created <= '$current_timestamp' ");
Considering the time values in my database are in "Y-m-d h:i:a" format, can I use "Y-m-d" for date comparison? Is it possible to do a MySQL query that involves both LIKE and <= ?
No need to do a str_replace() - just get the Y-m-d:
$start_date = date('Y-m-d', strtotime('last Sunday', strtotime($timestamp)));
Then, instead of manually creating a query, use the CakePHP conventions (yes, you can use Y-m-d for date comparison even though the datetimes stored in the database are Y-m-d H:i:s)
$this->Order->find('all', array(
'conditions' => array(
'id' => $id,
'created >=' => $start_date,
'created <=' => $end_date . ' 23:59:59',
'my_field LIKE' => '%whatever%'
));
Though - this seems kind of strange - usually you're either looking for something by 'id' OR by a date range - not both. But - maybe you have a reason :)
And as you can see above, yes, you can add a 'LIKE' also if you need.
Above answer is totally correct, but you can take easier approach using cakePHP time helper, which has function daysAsSql, it transcribes and time-readable strings into database range.
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql
Just add condition's array like this.
$resl = $this->DBNAME->find('all',array(conditions=>array('date1>date','date1<date')));
Replace 'date' with your date.
This is worked for me.
Try this in your controller
$searchTutorQuery = $this->Tutordetails->find('all', array(
'conditions' => array(
"User.zip" => $zipcode1,
"Tutordetails.user_id"=>$uid,
"Tutordetails.subject_id" => $subjectName,
"Tutordetails.hourly_rate >=" => $hourly_rate
),
//"User.id =" => $userid,
'fields' => array('user_id', 'Tutordetails.subject_id', 'Tutordetails.hourly_rate',
'User.zip', 'Tutordetails.zip'),
'order' => array('Tutordetails.id DESC')
));

How to display only date in symfony sfWidgetFormDate but it should pass month and day as default '1'?

I have to display date alone in sfWidgetFormDate but I want date and month to be passed in hidden.
I have given as:
$this->widgetSchema['fellowship_admission_date'] = new sfWidgetFormFilterDate(array(
'from_date' => new sfWidgetFormDate(array('format' => '%year%')),
'to_date' => new sfWidgetFormDate(array('format' => '%year%')),
'with_empty' => false
))
It displaying only year but I want to pass month and day in 'from_date' and 'to_date' default as '1' as hidden.
Why don't you display the entire date, and hide day and month with jQuery?!
It could be a good and fast solutions...

Insert Date into database table using Zend framework insert method

I have the foll code as:
$table_project_win = new Application_Model_DbTable_AfterWinProject();
$data_win = array(
'project_id' => $project_id,
'project_name' => $project,
'project_type_id' => $pro_type,
'start_date' => $dateStart,
'end_date' => $dateEnd,
'project_size' => $size,
'project_description' => $pro_des
);
$table_project_win->insert($data_win);
Here I get the $dateStart and $dateEnd variabled using as:
$dateStartt = $this->_getParam('dateStart');
echo 'date Start: '.$dateStartt;
$dateStart='"'.$dateStartt.'"';
$dateEndd = $this->_getParam('dateEnd');
$dateEnd='"'.$dateEndd.'"'
By using getParam I get the value of the date that the user has given input But when i will insert it into the database I use as
$dateStart='"'.$dateStartt.'"';
$dateEnd='"'.$dateEndd.'"'
But in the database table the value for date inserted is '0000-00-00' While when I echo the $dateStart which I have got through getParam It gives the correct value as '2012-12-11'.What is the reason of it??What Should I do??
replace $dateStart='"'.$dateStartt.'"';
with
$dateStart= $dateStartt ;
or
$dateStart='`'.$dateStartt.'`';

PHP date() function inserting zeros into database

I have an array like so which has the column names of the table on the left and the associating values on the right.
$proceduredata = array
(
'patient_id' => $patientfk,
'name_id' => $procedurenamefk,
'department_id' => $departmentfk,
'dosage_id' => $dosagefk,
'edocument' => NULL, //not implemented yet
'user_id' => $this->session->userdata('userID'),
'duration' => NULL, //not implemented yet
'submitted' => date('d-m-Y H:i:s', now()),
'comment' => NULL, //to be implemented
);
This array is then passed into a SQL insert function. The insert works fine but my "Submitted" column is getting values of this only:
0000-00-00 00:00:00
I made sure the time formats are matching? Is there something I have missed thanks.
change the date format
submitted' => date('Y-m-d H:i:s', now())
I believe that the "now()" function is a sql function and you are using PHP with the "date()" funciton. Try changing "now()" to "time()" which will give you a proper unix timestamp that the "date()" function can use to create a properly formatted date.
EDIT: I just realized I am not familiar with codeignighter, so please excuse me if the "now()" function is part of that framework.
now() is not a function, change now() to time(). Alternatively, date()'s second paramater is optional if you omit it date automatically uses the current date/time:
echo date('d-m-Y H:i:s');
will echo the current date/time in the format requested.

Drupal Date Filed Problem

I am using custom module in drupal and i am working in hook_form_alter() .
I am passing date format as this type.
But it is not working and filed type is DATETIME.
Please help me...
$form['field_res_inq_time_arrival'][0]['#default_value']['value'] = date('d/m/Y - h:m',strtotime('2011/04/13'));
Regards
Aasim Afridi
From Form Api Reference:
The #default_value will be today's date if no value is supplied. The format for the #default_value and the #return_value is an array with three elements with the keys: 'year', month', and 'day'. For example, array('year' => 2007, 'month' => 2, 'day' => 15)

Categories