我目前正在开发一款android应用程序,该应用程序使用PHP从SQL数据库中检索数据。每一组数据都得到了不同的时间戳。我的数据库中时间戳的格式是(yyyy-mm-dd-h:m:s(。现在我想转换我的时间戳,这样Android用户就会看到时间戳,比如(X秒前,X分钟前或X天前(。我尝试了php代码,但从每组数据的时间戳中得到的结果是(1天前(。每组数据都显示在1天前。有人能帮我吗?感谢
Product.php
<?php
$response = array();
// include db connect class
require_once 'include/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM image_detail ORDER BY posted_at DESC") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["uid"] = $row["uid"];
$product["itemname"] = $row["itemname"];
$product["price"] = $row["price"];
$product["description"] = $row["description"];
$product["path"] = $row["path"];
$timestamp = $row["posted_at"];
$now = date("Y-m-d h:i:s");
$product["posted_at"] = xTimeAgo($timestamp, $now, "x");
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
function xTimeAgo ($oldTime, $newTime, $timeType) {
$timeCalc = strtotime($newTime) - strtotime($oldTime);
if ($timeType == "x") {
if ($timeCalc = 60) {
$timeType = "m";
}
if ($timeCalc = (60*60)) {
$timeType = "h";
}
if ($timeCalc = (60*60*24)) {
$timeType = "d";
}
}
if ($timeType == "s") {
$timeCalc .= " seconds ago";
}
if ($timeType == "m") {
$timeCalc = round($timeCalc/60) . " minutes ago";
}
if ($timeType == "h") {
$timeCalc = round($timeCalc/60/60) . " hours ago";
}
if ($timeType == "d") {
$timeCalc = round($timeCalc/60/60/24) . " days ago";
}
return $timeCalc;
}
?>
由于以下代码块,代码总是有一天返回:
if ($timeType == "x") {
if ($timeCalc = 60) {
$timeType = "m";
}
if ($timeCalc = (60*60)) {
$timeType = "h";
}
if ($timeCalc = (60*60*24)) {
$timeType = "d";
}
}
($timeCalc=60(没有检查$timeCalc是否等于60,这也是错误的,它应该检查它是否大于60,它将$timeCalc设置为60,这也被评估为true。
因此,每次运行函数时,$timeCalc首先设置为60,然后是60*60,最后是60*60*24。与$timeType类似,它被设置为"m",然后是"h",最后是"d",这样,如果你检查你的代码,你就会明白为什么它总是有一天会返回。
请尝试设置以下if子句:
if ($timeType == "x") {
if ($timeCalc > 60) {
$timeType = "m";
}
if ($timeCalc > (60*60)) {
$timeType = "h";
}
if ($timeCalc > (60*60*24)) {
$timeType = "d";
}
}
if ($timeType == "x") {
if ($timeCalc = 60) {
$timeType = "m";
}
if ($timeCalc = (60*60)) {
$timeType = "h";
}
if ($timeCalc = (60*60*24)) {
$timeType = "d";
}
}
你有个问题,您在if:中影响$timeCalc
$timeCalc = 60
举个例子,我认为你完全忘记了==如果.
更正代码:
if ($timeType == "x") {
if ($timeCalc == 60) {
$timeType = "m";
}
if ($timeCalc == (60*60)) {
$timeType = "h";
}
if ($timeCalc == (60*60*24)) {
$timeType = "d";
}
}
相同的简化脚本将是
function xTimeAgo ($oldTime, $newTime) {
$timeCalc = strtotime($newTime) – strtotime($oldTime);
if ($timeCalc > (60*60*24)) {$timeCalc = round($timeCalc/60/60/24) . ” days ago”;}
else if ($timeCalc > (60*60)) {$timeCalc = round($timeCalc/60/60) . ” hours ago”;}
else if ($timeCalc > 60) {$timeCalc = round($timeCalc/60) . ” minutes ago”;}
else if ($timeCalc > 0) {$timeCalc .= ” seconds ago”;}
return $timeCalc;
}
试试这个。希望这对你有帮助。
有一个很好的代码示例