Translate month in Symfony 2 date field - php

I'm working on SF2 2.6.4 project.
I created a form and added a date type component (birthday field type to be precise).
I set it like that :
'widget' => 'choice',
'format' => 'ddMMMMyyyy',
Format output
18 March 2015
I would like to translate month.
During my research, I saw that Symfony\Component\Form\Extension\Core\Type\DateType class set a formatter with the hardcoded \Locale::getDefault() variable system.
So is there a way to do what I want ?
Is it a better idea to change my date format ?
Thx

Normally if you have your intl enabled in php (phpinfo) the form translate the months according to your locale configuration try setting locale like this before your form creation :
setlocale(LC_TIME, "fr_FR");
Another alternative if this doesn't help is this :
http://sonata-project.org/bundles/intl/master/doc/reference/datetime.html you can use the datetime helper

Related

Carbon get month index from month name

I'd like to get month index from month name using Carbon.
But I use Turkish month names.
I pass a query string to index like this ?ay=Temmuz&yıl=2017 so carbon should give 7 in this case.
The relevant part in my index function is like this:
public function index()
{
$gonderiler = Gonderi::latest();
if ($ay = request('ay'))
{
Carbon::setLocale(config('app.locale'));
$gonderiler->whereMonth('created_at', Carbon::parse($ay)->month);
}
if ($yil = request('yil'))
{
$gonderiler->whereYear('created_at', $yil);
}
/ ... /
}
When I click side bar and pass this query string it gives an error message like that:
"DateTime::__construct(): Failed to parse time string (Temmuz) at
position 0 (T): The timezone could not be found in the database"
on Linux
If you have trouble with translations, check locales installed in your system (local and production).
locale -a to list locales enabled.
sudo locale-gen tr_TR.UTF-8 to install a new locale.
sudo dpkg-reconfigure locales to publish all locale enabled.
And reboot your system. see documentation: http://carbon.nesbot.com/docs/#api-localization
For more you can refer to: Laravel Carbon localization not working (get localized name of month from number)
As the error you are getting is about Timezone not found in database.

I am Facing issue in magento timezone when someone place order.

Timezone Issue is inside the order email.
I put My code which returns date like :
Code :
$created = Mage::helper('core')->formatDate($this->getCreatedAt(), 'long', true);
O/P :
March 31, 2016 9:10:33 AM EDT
But I want to Need Date Like Based on EST Timezone :
EST - Timezone with proper
So please any one can help me how can i achieve this and also first priority to magento code and second one PHP.
Thank You.
Login as admin and navigate to System -> Configuration -> General -> Locale Options -> Timezone and change timezone from there

/DateTime field for birthday in symfony forms

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.

symfony2 - form with date field

I have a problem with a symfony form and a date field. When I view the field, the value retrieved from the database is subtracted a day when it is rendered as a widget single_text and I can not fix it.
For the project I use Symfony 2.6Beta for Bootstrap form support.
The field in form is defined:
$builder->add('natoIl', 'birthday', array(
'widget' => 'single_text',
'empty_value' => '',
'format' => 'dd/MM/yyyy',
'model_timezone' => 'Europe/Rome',
'view_timezone' => 'Europe/Rome'
));
I have this problem with MySQL and PostgreSQL.
Thank you very much
EDIT:
The problem occurs only with the 'widget' option set to "single_text". I need to see it as text input to add a datepicker field.
EDIT #2:
Doing various tests I found that the problem is when I work with the PHP web server. PHP version I use is 5.5.9-1ubuntu4.5 PC with linux mint.
Possibly you need to set your view and/or model timezone appropriately:
http://symfony.com/doc/current/reference/forms/types/date.html#view-timezone
This is a bug already fixed. The solution is then upgrading symfony.
https://github.com/symfony/symfony/issues/12808

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
));

Categories