Dates and Timestamps: What You Need to Know

Storing Dates and Timestamps

JSON has quickly displaced XML over the past few years, primarily due to the rise of internet technologies. JSON stands for JavaScript Object Notation and is a standard data serialization method for representing arbitrarily complex data(JavaScript Object) as a series of characters (JSON String)

JavaScript objects are pieces of data that can hold a number of key/ value pairs known as properties and property values. It is useful to start any introduction to JSON with the building block object types(numbers, strings, arrays, and freeform objects) that can be combined and nested to describe almost any data, regardless of its complexity.


The following are supported JSON types:

1. Numbers

Either whole or decimal numeric types:

Examples: 7 and 13.27

JavaScript Code:
var numberObj = 17;
console.log(JSON.stringify(numberObj)); //Prints 17

PHP Code:
$numberObj = 17;
echo json_encode($numberObj); //Prints 17

2. Strings

A sequence of characters used to store text data.

Examples: "Tom" and "Bob Dylan". Notice the syntax in describing a string in JSON: simply surrounding the text phrase with double quotation marks defines a string object

JavaScript Code:
var stringObj = "John Lennon";
console.log(JSON.stringify(stringObj)); //Prints "John Lennon"

PHP Code:
$stringObj = "John Lennon";
echo json_encode($stringObj); //Prints "John Lennon"

3. Arrays

An ordered sequence of JSON objects

Examples include [1, 2, 3, 4, 5] and ["Blowin", "In", "The", "Wind"]. The syntax in describing a string in JSON is simply surrounding the text phrase with double quotation marks.

JavaScript Code:
var arrayObj = ["John", "Paul", "George"];
arrayObj.push("Ringo");
console.log(JSON.stringify(arrayObj)); //Prints: ["John", "Paul", "George", "Ringo"]

PHP Code:
$arrayObj = array("John", "Paul", "George");
$arrayObj[] = "Ringo";
echo json_encode($arrayObj); //Prints: ["John", "Paul", "George", "Ringo"]

4. Objects

A collection of key/ value pairs that define properties

Examples include {} and {"fName": "John", "lName":"Lennon"}. The properties are defined in comma-separated property:value format and surrounded by curly braces. {} represents an empty object with no properties defined

JavaScript Code:
var obj = {
"n":"The Rolling Stones",
"yearHit":1965,
"firstHitSong":"(I Can’t Get No) Satisfaction"
};
console.log(JSON.stringify(obj));
//Prints: "{"n":"The Rolling Stones","yearHit":1965,"firstHitSong":"(I Can’t Get No) Satisfaction"}"

PHP Code:
$obj = array(
"n"=>"The Rolling Stones",
"yearHit"=>1965,
"firstHitSong"=>"(I Can’t Get No) Satisfaction"
);
echo json_encode($obj);
//Prints: "{"n":"The Rolling Stones","yearHit":1965,"firstHitSong":"(I Can’t Get No) Satisfaction"}"

A Multi-Language Example: JavaScript PHP

JavaScript

var TheBeatles = {
"artist":"The Beatles",
"firstHit": {
"title":"Love Me Do",
"single":true,
"year":1966
},
"lastHit": {
"title":"The Long And Winding Road",
"single":false,
"album":"Let It Be"
}
};

JSON.stringify(TheBeatles)
"{"artist":"The Beatles","firstHit":{"title":"Love Me Do","single":true,"year":1966},"lastHit":{"title":"The Long And Winding Road","single":false,"album":"Let It Be"}}"

PHP

(Assumes that we've AJAXed the Artist JSON string to the server)
function getFirstHitTitle(artistJSONString) {
$artistObj = json_decode(artistJSONString, true);
if(isset($artistObj["firstHit"])) {
echo "First hit for " . $artistObj["artist"] . " is " . $artistObj["firstHit"]["title"];
} else {
echo "No First Hit";
}
}


//First hit for The Beatles is Love Me Do