/DateTime field for birthday in symfony forms - php

I need to retrieve the birthday of a student from the database and set it as the birthday of a student entity. Then I need to load that data to a form.
$birth_day = strtotime($student_[0]['birthday']);
$birth__day = date('Y-m-d H:i:s',$birth_day);
$student->setBirthday($birth__day);
And in the form builder I have used the below code
add('birthday', 'birthday',array(
'data' => new \DateTime(),'placeholder'=>'-SELECT-'))
Instead of the birthday that particular student, it shows the today's date.
Can someone please explain what should I do to load the birthday instead of the today's date.
Thankyou in advance.

When you use the 'data' option, you are setting the default value for the field so in your example you are setting it to a new Datetime (today).
Here is the symfony doc page about it http://symfony.com/doc/current/reference/forms/types/form.html#data
Like #abdiel suggested, remove the data part.

Related

Moodle date selector (year only)

I am new to Moodle and need some assistance creating (year only) date selector.
In the code below I have a few entry fields.
If possible, I would like to customize the bottom 2 date selector fields to display only years for the user selection. My attempted workaround was to use the 'PARAM_INT'as a data type, which does not seem to be work. It also would not stop users from entering years such as 0000 or 1000. Thanks for your help in advance!
$mform->addElement('text', 'article_type', 'Article Type');
$mform->setType('article_type', PARAM_TEXT);
$mform->addElement('date_selector', 'print_article_date', 'Print Article Date');
$mform->setType('article_date', PARAM_INT);
$mform->addElement('date_selector', 'earliest_article_year', 'Earliest Article Year');
$mform->setType('earliest_publication_year', PARAM_INT);
Do you need a date selector? Maybe use a drop down select for the years instead and store the year rather than a date.
$options = array_combine(range(1900,2018), range(1900,2018));
$mform->addElement('select', 'earliest_article_year', 'Earliest Article Year', $options);
$mform->setType('earliest_publication_year', PARAM_INT);
Incidentally, you should store strings in the language file.
If its a local plugin, this the language file would be /local/yourplugin/lang/en/local_yourplugin.php
Then have something like this in the language file:
$string['earliestyear'] = 'Earliest Article Year';
Then change your form code to:
$mform->addElement('select', 'earliest_article_year',
get_string('earliestyear', 'local_yourplugin'), $options);

Magento 1.x Date field is not getting updated when we use setData method with multiple setData

We are using Magento 1.9 for our application. Here is my sample code
$customer_collection= Mage::getModel("customer/customer")->load($data['customer_id']);
foreach ($data['data'] as $key => $customer) {
$customer_collection->setData($customer['attribute_name'] , $customer['attribute_value']);
}
$customer_collection->save(); //finally saving the data
Above code is working for all the fields except date field. Issue is when we send multiple data including date fields, other fields are getting updated but date field is not getting updated. Can anyone help me to solve this issue?
For date field update try to use
$object->setData($attributeCode, Varien_Date::now());
As #mladen-ilić Suggested,
I did Flush Cache Storage and tried again to post the data. It works like a charm.

In mysql date field saving wrong year

I'm using MySQL to store my data using PHP. In my table, I have two fields as date (Start_date and End_date). I'm having issue on those fields while saving the data. I gave the input as following
start_date: 2016-12-03 it saved as ---> 2192-12-03
I don't now what is the issue please anyone help in that.
sample code for inserting in cake php 3
$add_val= $this->school->newEntity();
$save_val= $this->school->patchEntity($add_val, $this->request->data);
if ($save = $this->school->save( $save_val))
{
$passmessage = ['status' => 'success];
}
school is the table name.

Converting from typical input "dd/mm/aaaa" to valid Symfony values

I've a table where I set a field as TIMESTAMP, then I generate a module using tasks doctrine:generate-admin. By default Symfony creates a _form.php template where TIMESTAMP fields come separately as for example:
alumnos_fecha_ingreso_day
alumnos_fecha_ingreso_month
alumnos_fecha_ingreso_year
alumnos_fecha_ingreso_hour
alumnos_fecha_ingreso_minute
So in my template I've a field where values are entered as "dd/mm/yyyy" (is the default format) and I don't want to use Symfony way (it's ugly for end users). My question is how I deal with this value in order to get the right value and Symfony pass form validation? Is there any way to hide Hour and Minutes fields? Or is complety necessary?
If you just want day/month/year, you have to define the option with_time to false inside your form class.
with_time: Whether to include time (true by default)
And in your form:
$this->widgetSchema['my_date'] = new sfWidgetFormDateTime(array(
// I don't remember if you need to re-define the format
'date' => array('format' => '%day%/%month%/%year%'),
'with_time' => false
));

Customize the datetime format in Cakephp

When I use the form helper to make a time input,usaually I write code as follows
<?php
$options = array(
'label' => '',
'type' => 'datetime',
'timeFormat'=>'24',
'separator'=>'-'
);
echo $form->input('Service.endtime',$options);
?>
But now I get a problem that I want to make a time input style such as
month-day-hour
Then how can do this with setting some parameters in the helper?Suggestions would be appreciated.
Well dateformat option will allow you to configure the date parts.
Used to specify the format of the select inputs for a date-related set of inputs. Valid values include ‘DMY’, ‘MDY’, ‘YMD’, and ‘NONE’.
http://book.cakephp.org/view/203/options-dateFormat
However for this I think that you will need to extend the helper and create a custom function which will return this mixed date and time style. Otherwise I can only think of having two selects, one with month and day, and one for hour. Then merging the data somewhere else, perhaps in beforeSave() or beforeValidate()
Hope my solution may help someone with the same requirement:
<?php
//echo $form->year('Modelname.year',2001,2021,intval(date('Y')),false)."-";
echo $form->month('Modelname.month',date('m'),'',false)."-";
echo $form->day('Modelname.day',date('d'))."-";
echo $form->hour('Modelname.hour',1,date('H'));
?>

Categories