Monday, January 13, 2020

Moment js examples

moment().format('MMMM Do YYYY, h:mm:ss a')January 13th 2020, 3:19:07 pm
moment().format('dddd')Monday
moment().format('MMM Do YY')Jan 13th 20
moment().format('YYYY [escaped] YYYY')2020 escaped 2020
moment().format()2020-01-13T15:19:07+06:00
moment(new Date()).format('MMMM Do YYYY, h:mm:ss a')January 13th 2020, 3:19:07 pm
moment(new Date(2012,12,3)) < moment(new Date(2012,12,1))false
moment(new Date(2012,12,3)) < moment(new Date(2012,12,4))true
moment('2014-02-17T08:30:00').format('MMMM Do YYYY, h:mm:ss a')February 17th 2014, 8:30:00 am

MomentResult
moment('20111031', 'YYYYMMDD').fromNow()8 years ago
moment('20120620', 'YYYYMMDD').fromNow()8 years ago
moment().startOf('day').fromNow()15 hours ago
moment().endOf('day').fromNow()in 9 hours
moment().startOf('hour').fromNow()19 minutes ago

MomentResult
moment().format('MMMM Do YYYY, h:mm:ss a')January 13th 2020, 3:19:07 pm
moment().add('days', 7).format('MMMM Do YYYY, h:mm:ss a')January 20th 2020, 3:19:07 pm
moment().add('days', 7).add('months', 1).format('MMMM Do YYYY, h:mm:ss a')February 20th 2020, 3:19:07 pm
moment().add({days:7,months:1}).format('MMMM Do YYYY, h:mm:ss a')February 20th 2020, 3:19:07 pm
moment().add('milliseconds', 1000000).format('MMMM Do YYYY, h:mm:ss a')January 13th 2020, 3:35:47 pm
moment().add('days', 360).format('MMMM Do YYYY, h:mm:ss a')January 7th 2021, 3:19:07 pm
moment(new Date(2012,12,3,6,0,0,0)) - moment(new Date(2012,12,3,5,0,0,0))3600000
(new Date(2012,12,3,6,0,0,0)) - (new Date(2012,12,3,5,0,0,0))3600000
new Date().getTimezoneOffset()-360
moment(new Date()).zone()-360
(new Date(2013,2,1,14,22,0,0)) - (new Date(2013,1,30,13,22,0,0))-82800000
moment('5/17/2013 1:45PM', 'DD/MM/YYYY hh:mm:ssa').format('MMMM Do YYYY, h:mm:ss a')May 5th 2014, 1:45:00 pm
(moment('2014-02-17T16:30:00')).format('MMMM Do YYYY, h:mm:ss a')February 17th 2014, 4:30:00 pm
(moment('2014-02-17T16:30:00z')).format('MMMM Do YYYY, h:mm:ss a')February 17th 2014, 10:30:00 pm
new Date('2014-02-17T16:30:00z')Mon Feb 17 2014 22:30:00 GMT+0600 (Bangladesh Standard Time)

https://codepen.io/bassfiddle/pen/lrLwy?__cf_chl_jschl_tk__=0943c67cf5ff73480726a5ac8e5763ebb56522ae-1578907142-0-AbBkBcHLw26HIyPKro4vU7EsZF5vhL3G8LVFxWAC2jT37WBmIFhcNIEgck9E8cAdeMLH1NQ47enPSkDkt450LHjXG1pPpY18RU1VY7yM-bKr97mqZCW8ZZ53Okv6V-yQ7MB_do_Y74h6f7VNCcPTTcKFkXrRZVAgDARL0BeYdl4oeS0pTZxGNiwwWTpNrcmFys5TKpyZzqse4t6LfZfW2Tgbn1KohgagcD687HA9pU2P89A42M3wongqOLd-UDwoHcZ-XVxuTPIeVwCtYNGdG2ZAR4Lhv0wbdw5K0T3I0cB2bUShXNoS-h-xevErZnZNO0dvH7Z8bcJIRykjtU2dsFy5I7mgeWj4i6ngs-CU6P4l

Friday, December 27, 2019

Read/Write JSON Files with Node.js

How to read and write JSON files in PHP

n a previous post we explained the characteristics of the JSON format. Using JSON, complex data structures can be saved in a plain text file, readable by a person.
In this post we will show how to save and read a JSON-formatted data file from a PHP script.
We will use as an example a file named “data.json” with the following contents:

PHP 5.4

The PHP script below reads a JSON file, modifies the value of one of the elements in the data structure read, and writes the modified data structure to another file:
In line 4 the file contents are read and assigned as a string to the  $str_data variable.
In line 5 the  ‘json_decode’ method is called. This method parses the string into a PHP data structure. Both the associative arrays (Enclosed in curly braces ‘{‘ and ‘}’ in the input file ), and the ‘standard’ arrays (Enclosed in square brackets ‘[‘ and ‘]’ in the input file) are converted into PHP arrays.
In line 7 we see how one of the values inside the data structure can be accessed, by navigating the array tree.
In line 10 the value is modified, and in the next lines the ‘json_encode’ method is used to convert the data structure back into a JSON-formatted string, which is then written into an output file ‘data_out.json’.
As we can see, the call to ‘json_encode’, specifies the ‘JSON_UNESCAPED_UNICODE’ flag. This flag is available since PHP 5.4, and is relevant if we are working with data that includes strings with characteres outside de ASCII set, normally found in most languages other than english.
Without this flag, json_encode converts any non-ASCII character found into unicode escape sequences. For instance, the string ‘Música’ would be written as “Mu00fasica”. Although this is a valid unicode text (and can be read back by json_decode into a PHP variable), it is not well suited to be read by a person. The flag JSON_UNESCAPED_UNICODE ensures that these characters are converted into their equivalent UTF8-encoded character. In this way, any text editor supporting UTF-8 encoding will display the string as “Música”.

PHP versions prior to 5.4

If the PHP version we are using is earlier than 5.4, we can’t use the JSON_UNESCAPED_UNICODE flag.
For this reason, if working with unicode escape sequences is not an option, we must write our own conversion routine to convert from unicode escape sequences to UTF-8. The new script would be like this:
https://blog-en.openalfa.com/how-to-read-and-write-json-files-in-php