我将数据从服务器发送到android并且我在数据库中有一个字段作为时间戳,我也发送数据作为JSON但是当我发送数据时它显示的日期如下:date“:1463232118”或者当我从它显示日期的代码中删除'strtotime'函数,如下所示:2016-05-18 10:24:32但是当我把'strtotime'放在将要发送到android端的JSON数组时,我打开它浏览器它显示如下:4周前或3天前它是正确的但当我发送它作为JSON数组时它显示:日期“:1463232118”。这是我完整的PHP代码指南,或者添加你知道的东西
<?php
require_once("include.php");
$tbl_name ="ads";
$order_by ="id";
$records_limit= 6;
$connect = @mysqli_connect($hostname , $username , $password , $database);
if( $connect )
{
@mysqli_query($connect , "SET CHARACTER SET utf8;");
$temp1 = @mysqli_query($connect , "SELECT COUNT (*) FROM".$tbl_name);
$temp2 = @mysqli_fetch_row($temp1);
$total_records = $temp2[0];
if(isset( $_GET['page']))
{
$page = $_GET['page'];
$offset = $page * $records_limit;
}
else
{
$page = 0;
$offset = 0;
}
$my_query = "SELECT * FROM ".$tbl_name." ORDER BY ".$order_by." DESC LIMIT ".$offset.",".$records_limit;
$result = @mysqli_query($connect , $my_query);
if( $result )
{
$response['ads'] = array();
$response['success'] = 1;
while( $row = @mysqli_fetch_array($result) )
{
$ads = array();
$curenttime=$row['date'];
$time_ago =strtotime($curenttime);
$ads['id'] = $row['id'];
$ads['title'] = $row['title'];
$ads['intro'] = $row['intro'];
$ads['image'] = $sitename . $row['image'];
$ads['seller'] = $row['seller'];
$ads['phone'] = $row['phone'];
$ads['date'] = $time_ago;
$q = "SELECT name FROM cat WHERE id='".$row['cat_id']."'";
$temp1 = @mysqli_query($connect , $q);
$temp2 = @mysqli_fetch_row($temp1);
$cat = $temp2[0];
$ads['cat'] = $cat;
array_push( $response['ads'] , $ads);
}
}
else
{
$response['success'] = 0;
$response['message'] = "nothing";
}
echo(json_encode($response));
@mysqli_close( $connect );
}
function timeAgo($time_ago){
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
echo "one minute ago";
}
else{
echo "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
echo "an hour ago";
}else{
echo "$hours hours ago";
}
}
//Days
else if($days <= 7){
if($days==1){
echo "yesterday";
}else{
echo "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
echo "a week ago";
}else{
echo "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
echo "a month ago";
}else{
echo "$months months ago";
}
}
//Years
else{
if($years==1){
echo "one year ago";
}else{
echo "$years years ago";
}
}
}
?>
以下是我将$curenttime=$ads['date'];
$time_ago =strtotime($curenttime);
echo timeAgo($time_ago);
放在数组外的输出。
5 hours ago5 hours ago4 days ago4 days ago4 days ago
答案 0 :(得分:3)
timeAgo
函数应return
值,而不是echo
。
你忘了给$ads['date'] = timeAgo($time_ago);
打电话,这就是为什么你的JSON中有strtotime
值而不是之前的转换值。
修改强>
要从timeAgo()
函数返回值,只需执行以下操作:
return $seconds . " seconds ago";