Yii2 formatter language - yii2

This is my configurations:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'd MMMM Y',
'locale' => 'ru-RU'
],
When I am trying this:
echo Yii::$app->formatter->asDate('2014-01-01');
I get:
01 Jan 2014
But I want the same in Russian.
Solved:
Open the file php.ini via OpenServer and uncomment ;extension=php_intl.dll.

the config you specified should output 01 янв. 2014
you are most likely missing php-intl extension

With formatter set to 'dateFormat' => 'php:d M Y' you can only get 01 Jan 2014 because PHP's date M stands for
A short textual representation of a month, three letters: Jan through Dec
If you want date to be formatted by intl for your language you need to set formatter with ICU:
'dateFormat' => 'd MMM Y'
like described at ICU User Guide.

Related

JSON string to date with Javascript in Google Apps Script editor

I am working through w3schools, particularly https://www.w3schools.com/js/js_json_parse.asp
I ran this example and got an unexpected result
let dashText = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';
let objD = JSON.parse(dashText);
console.log("objD: ", objD);
objD.birth = new Date(objD.birth);
console.log("objD.birth: ", objD.birth);
3:09:04 PM Info objD: { name: 'John', birth: '1986-12-14', city: 'New York' }
3:09:04 PM Info objD.birth: Sat Dec 13 1986 18:00:00 GMT-0600 (Central Standard Time)
Note the difference in the dates. I then changed the dashes to slashes out of curiosity and the date was correctly determined from the string.
let slashText = '{ "name":"John", "birth":"1986/12/14", "city":"New York"}';
let objS = JSON.parse(slashText);
console.log("objS: ", objS);
objS.birth = new Date(objS.birth);
console.log("objS.birth: ", objS.birth);
3:09:04 PM Info objS: { name: 'John', birth: '1986/12/14', city: 'New York' }
3:09:04 PM Info objS.birth: Sun Dec 14 1986 00:00:00 GMT-0600 (Central Standard Time)
Can anyone explain the results?
Javascript parses DateTime strings differently based on how the string is formatted. The dashes are parsed to ISO date, i.e. international time. You can see this when it tries to handle the timezone conversion, where it sets the time to 18:00:00 to account for the 6 hour shift from Universal Time. Slashes are parsed as just the date, and doesn't try to adjust the time based on timezones.
Here's a w3schools link that goes over this in more detail.

How to render correct datetime in hugo post?

I created a post using hugo new posts/mypost.md and it created mypost.md for me with header toml configuration like that
But, when i run on server (local), the datetime rendered wrong like:
How can I fix that?
Thanks in advance!
This is how I got it to work:
Add your date format in config.toml
[params]
dateFormat = "02 Jan 2006"
Your post should contain the date in its front-matter:
---
date: "2020-12-23T15:21:54+05:30"
...
---
Use the format in your layout:
<div>{{ .Params.date.Format .Site.Params.dateFormat }}</div>
Note: Please DO NOT change the numbers in the date format. It must be 02 for day, Jan for month, 2006 for year, etc. check this for more details.
Check also Hugo 0.87 (Aug. 2021, two years later), which comes with:
a timezone
a timedate format
{{ time.Format "Monday, Jan 2, 2006" "2015-01-21" }}
→
"Wednesday, Jan 21, 2015"
Note that since Hugo 0.87.0, time.Format will return a localized string for the current language.
Date/time formatting layouts
{{ .Date | time.Format ":date_long" }}
The full list of custom layouts with examples for English:
:date_full => Wednesday, June 6, 2018
:date_long => June 6, 2018
:date_medium => Jun 6, 2018
:date_short => 6/6/18
:time_full => 2:09:37 am UTC
:time_long => 2:09:37 am UTC
:time_medium => 2:09:37 am
:time_short => 2:09 am
In Hugo v0.95.0 (released 16 Mar 2022), it should be
[params]
date_format = "02 Jan 2006"
with
---
date: 2022-03-18T21:10:00+07:00
---
in the post front matter, where double quote marks are not necessary. I used Ananke theme.
Setting the date_format in the config file seems weird to me. I would set the following in the config file:
defaultContentLanguage: nl
languageCode: nl_NL
These are variables that do NOT go into params, but on the root level of the config file. When you have these you can simply call:
{{ .Date | time.Format ":date_long" }}
or... if you need another format:
{{ .Date | time.Format ":date_medium" }}
The date will be formatted in your language.
You need to set dateformat in file config.toml at 2.1.2006 (any proper format, ensure 2, Jan, 2006).
This link save my day

How to escape a string in Yii 2 formatter component?

I have the following configuration for the formatter component in my project:
'formatter' => [
'dateFormat' => 'dd.MM.yyyy',
'datetimeFormat' => 'dd.MM.yyyy HH:mm',
'decimalSeparator' => '.',
'thousandSeparator' => ' ',
'currencyCode' => 'EUR',
],
Now, for my datetimeFormat I'd like to suffix it with an h letter/string at the end of it.
I have tried with:
'datetimeFormat' => 'dd.MM.yyyy HH:mm\\h',
But the escaped \\h returns a letter g there. I know that \\h works with date.
How do we do escapes with Yii 2?
Okay, found it while reading formatter documentation and ICU manual.
To achieve this, escape the letter with wrapping your letter/string with single quotes:
'datetimeFormat' => "dd.MM.yyyy HH:mm'h'",
Sample output: 30.12.1990 12:25h.

Datetime format is not same with database in Yii2

I want to make datetime format in my view in yii2 project is not same with datetime in my database. I use this code:
return Yii::$app->formatter->asDatetime($model->tanggal_sampai, "php:d M Y H:i");
The datetime in database is : 2016-06-14 16:53:40
But when I see the result of the code above in yii2, the result is not same. The result is : 14 Jun 2016 18:53
There is no problem with the date, but the time is very different. What's the problem? I use format 'WIB' because I'm in Indonesia.
In your main config file, put:
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'php:m/d/Y',
'datetimeFormat' => 'php:Y-m-d H:i:s',
'timeFormat' => 'php:H:i:s',
],
And you will get:
echo Yii::$app->formatter->asDatetime('2016-06-14 16:53:40');
2016-06-14 18:53:40
It takes the time as UTC, and adds +2 because of my Timezone. If you define date_default_timezone_set in index as UTC or Yii::$app->timeZone = 'UTC' it won't transform the time you've saved in your database.
2016-06-14 16:53:40
INFO: Timezones for Asia http://php.net/manual/en/timezones.asia.php

insert assocative array data into mysql

I have the following twitter data output in an associative array format in php, how can I insert this data into mysql database table. Kindly suggest any way to do this.
Array (
[created_at] => Fri, 02 Nov 2012 18:15:57 +0000
[from_user] => VolksblattNews
[from_user_id] => 828753642
[from_user_id_str] => 828753642
[from_user_name] => Volksblatt News
[geo] => [id] => 2.6443066310356E+17
[id_str] => 264430663103557632
[iso_language_code] => de
[metadata] => Array ( [result_type] => recent )
[profile_image_url] => http://a0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif
[profile_image_url_https] => https://si0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif
[source] => <a Google</a> => Bundesrat Burkhalter verstärkt Schweizer Beziehungen zu Burma: RANGUN - Didier Burkhalter hat… http://t.co/BF3aX2bY
[to_user] =>
[to_user_id] => 0
[to_user_id_str] => 0 [
to_user_name] => )
I got this output from the following program:
<?php
//a json string
$jsonstring='{
"created_at": "Fri, 02 Nov 2012 18:15:57 +0000",
"from_user": "VolksblattNews",
"from_user_id": 828753642,
"from_user_id_str": "828753642",
"from_user_name": "Volksblatt News",
"geo": null,
"id": 264430663103557632,
"id_str": "264430663103557632",
"iso_language_code": "de",
"metadata": {
"result_type": "recent"
},
"profile_image_url": "http://a0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif",
"profile_image_url_https": "https://si0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif",
"source": "<a href="http://www.google.com/">Google</a>",
"text": "Bundesrat Burkhalter verst\u00e4rkt Schweizer Beziehungen zu Burma: RANGUN - Didier Burkhalter hat\u2026 http://t.co/BF3aX2bY",
"to_user": null,
"to_user_id": 0,
"to_user_id_str": "0",
"to_user_name": null
}';
$arrayvariable=json_decode($jsonstring,true);
print_r ($arrayvariable);
?>
Regards
Well depending on the use you could store it in as a json string (in case you want to send it directly to the browser and work with it there) or create a table with all the array values as columns. Then do a insert.
To make a safe the insert or update you have a nice example here using prepared statements and PDO.
http://www.phpeveryday.com/articles/PDO-Insert-and-Update-Statement-Use-Prepared-Statement-P552.html