DBから次の日付を取得します
"2013-11-07 10:41:00"
SafariやFirefoxではなく、Chromeでのみ解析できるようです。
//まず、タイムスタンプが DB から取得され、ここの配列に入れられます
$getActivityUpdates1 = mysql_query("SELECT s.id, u.name, u.profilePic, s.userid, s.content, s.time1, s.imageKey FROM status_updates s
INNER JOIN users1 u ON u.id = s.userid
WHERE competitionId = '$competitionId' AND s.id > '$lastPollId' ORDER BY s.id DESC LIMIT 0, 20");
$results = array('items' => array());
while ($row = mysql_fetch_array($getActivityUpdates1))
{
$results['items'][] = array(
'statusid' => $row['id'],
'name' => $row['name'],
'profilePic' => $row['profilePic'],
'content' => $row['content'],
'time1' => $row['time1'],
'imageKey' => $row['imageKey'],
);
if ($lastPollId < $row["id"]) {
$lastPollId = $row["id"];
}
}
$results["lastPollId"] = $lastPollId;
die(json_encode($results));
//sparta.js - ここには、タイムスタンプを「きれいな日付」に変換する基本的な関数があります。
function getNiceDate(d) {
var date = new Date(d).add(-new Date().getTimezoneOffset()).minutes(); //Problem with the format here?
var now = new Date(); //Problem with the format here?
var minutesDifference = parseInt((now - date) / (60 * 1000));
if (minutesDifference < 2) {
return "Just now";
} else if (minutesDifference < 60) {
return minutesDifference + " minutes ago";
} else if (minutesDifference < 60 * 24) {
var hoursDifference = parseInt(minutesDifference / 60);
return hoursDifference + " hours ago";
}
return date.toString("yyyy-MM-dd HH:mm"); //Problem with the format here?
}
//次に、HTML を作成するときに、getNiceDate() 関数を呼び出します。//entryData.time1 は、「2013-11-07 10:41:00」の形式の DB からのタイムスタンプです。
var activityTimeElement = entry.find(".actTime");
activityTimeElement.text(getNiceDate(entryData.time1)); //function getNiceDate() used here.
activityTimeElement.attr("data-timestamp", entryData.time1);
//出力と期待される結果
In chrome : "35 minutes ago" or "Just now" or if more than a few hours ago, the timestamp.
In Safari : "NaN-NaN-NaN NaN:NaN"
In Firefox : "NaN-NaN-NaN NaN:NaN"