Symfony2, DateTime formatting with date and time together - php

I have created a field for publish date (with time), but I found only date formating. My time field is under my date field and it looks strange (for me), like this:
[DAY] [MONTH] [YEAR]
[HOURS]:[MINUTES]
How can i format it like this?
[DAY] [MONTH] [YEAR] - [HOURS]:[MINUTES]
There it is, what I now have in my Form builder:
$builder->add('published', 'datetime', array('label' => 'Date of publish', 'date_format' => 'ddMMMMyyyy', 'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'), 'invalid_message' => 'Date is not valid',))
PS: I'm using selection.

I have had the same problem once and solved it using the answer of this question:
Form theming datetime widget Symfony 2.
Also, find more info on the matter by browsing the following section of the Symfony2 official documentation.
How to customize Form Rendering.

Related

October / Wintercms : set default list filter values programatically

I'm using Wintercms (fork of Octobercms) to create a backend application which needs to display some data according to a date range.
I've used the list filters to be able to select custom date range: https://wintercms.com/docs/backend/lists#list-filters
But when we land on the list, I would like to have a default date range selected.
The rule would be: "From 1st of february to 31st of october of the current year":
I haven't found any way of doing so in the documentations nor in internet examples..
Calculate dynamic defaults by adding scope definition in ListFilterExtendScopes event: https://wintercms.com/docs/backend/lists#extend-filter-scopes
Default for daterange scope is array of :afterDate and :beforeDate values:
$filter->addScopes([
'latest' => [
'label' => 'Latest',
'type' => 'daterange',
'conditions' => 'latest >= \':afterDate\' AND latest <= \':beforeDate\'',
'yearRange' => '20',
'default' => [
0: Carbon::now()->subDays(10),
1: Carbon::now()->addDays(10),
],
],
]);

Cakephp 4 Form Helper: Create Individual dropdown for day, month and year

So, in CakePHP 3, to get something like this:
I would do this:
<?= $this->Form->date('dob', [
'templates' => ['inputContainer' => '{{content}}'], 'label' => false, 'class' => 'form-control', 'minYear' => 1940, 'maxYear' => date('Y') - 5,
'empty' => [
'year' => "Year", // The year select control has no option for empty value
'month' => 'Month', // The month select control does, though
'day' => 'Day', // The month select control does, though
],
]) ?>
However, if I try to do the same thing on Cakephp 4, I get this
I've gone through the docs here: https://book.cakephp.org/3/en/views/helpers/form.html#creating-date-time-related-controls and https://book.cakephp.org/4/en/views/helpers/form.html
Please how can I achieve same Cakephp 3 output on Cakephp 4
Did you try
'type' => 'select'
as documented?
Otherwise a custom template might help.
Alternatively, the dropdown default part was moved to Shim plugin
See https://github.com/dereuromark/cakephp-shim/blob/master/docs/View/Form.md
That should give you the 3.x way of creating such form elements.

Symfony 3 DateType year format

Hi I've got a problem with year format on DateType, let take a look on this image
I want to change year from AD convert to BE in Thailand format
So It's will look like this AD = 2017 convert to BE = 2560
Is anyone know how to change this thing
and this is my form type code.
->add('birthday', DateType::class, [
'required' => false,
'widget' => 'single_text',
'label' => 'Birthdate',
])
You'll likely want to use an external plugin to manage this as the Symfony default won't handle conversion of "base year."
Something like this will be where I'd look: https://github.com/jojosati/bootstrap-datepicker-thai/blob/thai/README-thai.md
And then in your builder object, I'd add a class to select the thai datepicker elements:
$builder->add('birthday', 'text', array(
'attr' => array(
'class' => 'thai-datepicker') //for example
)
);

Symfony DateTime before 1902

I currently code a symfony project that requires saving many dates below 1091. In MySQL, DateTime recording is good. But when I refreshed the page, the value displayed is wrong. I use a datetime field including the following code:
$builder->add(
'datetimepublish',
DateTimeType::class,
[
'model_timezone' => date_default_timezone_get(),
'view_timezone' => date_default_timezone_get(),
'widget' => 'single_text',
'format' => 'dd/MM/yyyy HH:mm',
'label' => 'Date et heure',
'attr' => [
'placeholder' => 'Date et heure',
],
]
);
This behavior would be a 'normal', I searched a bit in the vendor, and I think I find when it did that, the DataTransformer. How to cure it?
I have the same issue here and discovered that it's most likely you work on a 32-bit system and symfony somewhere uses a timestamp representation of the date, which php on 32-bit systems can't handle if the year is one before 1902.
It's amazing.
#see:
https://github.com/symfony/symfony/issues/5227

symfony DateTimeToTimestampTransformer

I am trying to get the unix timestamp but can't seems to get it.
I have form where the users need to select the date and time
$builder->add('time', 'datetime', array(
'input' => 'timestamp',
))
More info:
http://symfony.com/doc/current/reference/forms/types/datetime.html#input
But all i have is an array.. when i need an integet with unix timestamp
$time = $formData['time'];
print_r($time);
Output:
Array(
[date] => Array(
[year] => 2015
[month] => 6
[day] => 7
)
[time] => Array(
[hour] => 6
[minute] => 6
))
When I need something like 1443197171
I'm assuming you're looking at the POST data that comes back from the form which is different from the normalised data that the Symfony Forms component provides.
The visual representation is controlled by the widget option of the field, so in your case you probably want to set it to single_text like so:
$builder->add('time', 'datetime', array(
'input' => 'timestamp',
'widget' => 'single_text',
));
Even then you'll probably get back an array with separate date and time entries. What you want to do is get the "data" attribute from the form component, e.g.
$form->get('time')->getData();
This gets the field component and then retrieves the data. As the entry you linked to states:
The value that comes back from the form will also be normalized back into this format.
At this point it might be best to look into the model / view structure of the form components.

Categories