从date()

时间:2016-09-06 21:48:27

标签: php mysql date backslash

我试图将SQL日期时间转换为Y / m / d G:i:s格式。 它已经形成,但在每个前面都有一个额外的反斜杠...我已经尝试过str_replace和stripslashes而且没有它们有效...

数据:http://www.zewde.org/instagram/script_new/data.php

代码:

<?php
define('DB_NAME', 'FollowersCount');
define('DB_USER', '******');
define('DB_PASSWORD', '******');
define('DB_HOST', '*.*.*.*');

$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$connection) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("FollowersCount", $connection);

$sth = mysql_query("SELECT Date FROM Count ORDER BY Date");
$sthh = mysql_query("SELECT Count FROM Count ORDER BY Date");


$sthhh = mysql_query("select a.ID, a.Count,coalesce(a.Count -(select b.Count from Count b where b.ID = a.ID - 1), 5) as diff from Count a ORDER BY Date");


$rows = array();
while(($r = mysql_fetch_array($sth)) && ($rr = mysql_fetch_array($sthh)) && $rrr = mysql_fetch_array($sthhh)) 
{
    $temp_count = intval($rr['Count']); 
    $temp_date1 = $r['Date'];

    $myFormatForView = date("Y/m/d G:i:s", strtotime($temp_date1));
    $final = str_replace("\\", "", $myFormatForView); //Doesn't work, neither does stripslashes...


    $temp = array( $final, $temp_count);
    $temp_s = implode(", ", $temp);
    $rows['data'][][] = $temp_s;
}


$result = array();
array_push($result,$rows);
$Jz = json_encode($result, JSON_NUMERIC_CHECK);

echo $Jz;

mysql_close($connection);
?>

2 个答案:

答案 0 :(得分:4)

这是json_encode的错误。

json_encode() options

  

<强> JSON_UNESCAPED_SLASHES
  不要逃避/。从PHP 5.4.0开始提供。

所以

$Jz = json_encode($result, JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES );

应该摆脱那些反斜杠。

答案 1 :(得分:2)

更好地直接在查询中设置日期格式...

SELECT DATE_FORMAT(Date,'%Y/%m/%d %k:%i:%s') AS niceDate
FROM Count 
ORDER BY Date

有关所有格式化选项,请参阅here