I'm having a problem with the yii2 date formatter, it doesn't use the locale that I have set. The global language is set to de-DE, it seems to take effect everywhere except for the date formatter. I've tryed to set the formatter locale in the config file like this:
'formatter' => [
'dateFormat' => 'dd.MM.yyyy',
'decimalSeparator' => ',',
'thousandSeparator' => ' ',
'currencyCode' => 'EUR',
'locale'=>'de_DE'
],
and also directly before using the formatter in code:
Yii::$app->formatter->locale = 'de-DE';
echo Yii::$app->formatter->asDatetime('2014-01-01');
but it formats the date to Jan 1, 2014 10:04:36 AM.
Any ideas?
The param dateFormat only has effect for asDate(). For asDatetime() set datetimeFormat.
possible reason:
check and make sure that php-intl extension is installed.
see: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n#parameter-formatting
Related
I'm trying to have a time input in TYPO3 9 LTS working together with MySQL 5.7.24.
In the ext_tables.sql the field gets defined like this:
some_field time default NULL
In the TCA the field gets defined like this:
'some_field' => [
'exclude' => 1,
'label' => 'Some field',
'config' => [
'type' => 'input',
'dbType' => 'time',
'eval' => 'time',
],
],
When saving the record in the backend without a time input (which should be possible) I'm getting the error:
These fields of record 1 in table "some_table" have not been saved correctly: some_field! The values might have changed due to type casting of the database.
When looking at the database record the some_field field gets the value 00:00:00 (although the db default is NULL).
When selecting a time the record can be saved and opened without error.
Is this a bug in TYPO3 or how could I fix this behavior?
The bug can be solved by having the following eval:
'eval' => 'time,null',
That means you have given the wrong type for the value on your ext_tables.sql. Additionally, TYPO3 v9 has renderTypes.
Try something like that:
ext_tables.sql
begin int(11) DEFAULT '0' NOT NULL
TCA
'begin' => [
'exclude' => true,
'label' => 'LLL:EXT:your_ext/Resources/Private/Language/locallang_db.xlf:tx_yourext_domain_model_modelname.begin',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'size' => 10,
'eval' => 'datetime',
'default' => time()
],
],
Additional information!
If you want to display the time in FrontEnd you could use something like that
<f:format.date>{dateObject.begin}</f:format.date>
If you want to modify how it looks, you can use the format attribute as well:
<f:format.date format="%d. %B %Y">{dateObject.begin}</f:format.date>
More about that here: TYPO3 Date format
What will be the rules for my pretty url if I have the following scenario:
links like this where parameters may vary.
domain/?bt=<token>&e=<email>
or
domain/?lt=<token>&e=<email>
then should be processed in a controller/action. ie. mycontroller/get
Also, parameters should be accessible by $_GET inside the action.
the simplest way is based on the use of urlHelper
use yii\helpers\Url;
$myUrl = Url::to(['your_controller/your_action', 'bt' => 123, 'e' => 'myemail#gmail.com']);
Using the urlHelper function Url::to .. the url you need is properly formed depending of the urlManager configuration you have set in your config file
and the param a manager as show in the sample like entry in an array.
The post or get method is related to the type of metho you have in your ulr call if not other values are specified the url is formed as a get
and you can obtain the values you need in $_GET['bt'] and $_get['e']
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'call-backs/get',
'unsubscribes' => 'unsubscribes/get',
],
],
#scaisEdge, thank you for answering my question. maybe my question isn't that clear but this is the solution I made for my question after a hard find of clues and tips online.
All I wanted was that when a user clicks on a link, hitting the main page/main domain, it will go to my yii project (intended to be a webservice or an API like one) then will be handled by a precise controller and action.
'' => 'call-backs/get'
the code above answers the question. Cheers.
I have an array like this:
['valid_from' => '2016-02-01']
In my model I have the following validation rule
$validator->date('valid_from')->allowEmpty('valid_from');
When I try to patch the entity with the array I get this:
'valid_from' => object(Cake\I18n\FrozenDate) {
'time' => '2168-12-02T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
The column in MySQL is of the type date. (I don't want to use datetime as I don't need the time for calculations)
What am I doing wrong?
Think your issue is that your locale is misconfigured try one of these methods.
The default locale can be set in your config/bootstrap.php folder by using the following line:
ini_set('intl.default_locale', 'fr_FR');
Changing the Locale at Runtime
use Cake\I18n\I18n;
I18n::locale('de_DE');
Is there any way to validate time attribute In MYSQL column contain time datatype. SO i can just validate it through YII.
So is it possible? LIKE
"[['from_date', 'to_date'], 'date']"?
Try this in model rules
['attr', 'date', 'format'=>'H:i']
For more on this please refer
Use like:
[['from_date', 'to_date'], 'date', 'dateFormat' => 'H:i:s']
You can use format attribute for time validation. It will handle both date and time validation.
['attribute_name', 'date', 'format' => 'yyyy-M-d H:m:s'],
OR
[['attribute_name1', 'attribute_name2'], 'date', 'format' => 'H:i:s']
I have a form with a element called "price". I validate this element with the "float" validator. The thing is when I insert, for example:
12,50 => it is valid but when I try to save it on the DB (mysql) it is saved as "12.00"
So I wanna to change the decimal character from "," to ".". Does anybody knows how??
Note. If I put:
$price->addValidator('Float', 'de')
or
$validator = new Zend_Validate_Float(array('locale' => 'de'));
$price->addValidator($validator)
It does not work.
You can use a filter Zend_Filter LocalizedToNormalized to it will normalized you localized price according to the user's locale.
A typical price element would be like this one:
$price = new Zend_Form_Element_Text('price');
$price->setLabel('Price:')
->setRequired(true)
->setAttribs(array('required name' => 'price', 'maxlength' => '12'))
->addFilter('StripTags')
->addFilter('StringTrim')
->addFilter('pregReplace', array('match' => '/\s+/', 'replace' => ''))
->addFilter('LocalizedToNormalized')
->addValidator('stringLength', true, array(1, 12))
->addValidator('float', true, array('locale' => 'en_US'))
->addValidator('greaterThan', true, array('min' => 0));
$this->addElement($price);
Of course, you can improve it and add the validators/filters you need.