2016-09-06 134 views
-3

我試圖將SQL datetime轉換爲Y/m/d G:i:s格式。 它已經格式化,但在每一個FRONTSLASH面前的一個額外的反斜槓...我已經試過str_replace函數和的stripslashes和他們的非工作過......從date()中刪除反斜槓

數據: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); 
?> 
+0

你輸出JSON。 '\ /'是你如何用JSON字符串表示'/'。數據中沒有'''',那只是編碼。 – Quentin

+0

而在stackoverflow上,編輯它並沒有幫助你,它仍然存在。請求一個mod完全刪除該問題並更改密碼! – baao

+0

感謝您的幫助,tkausl的答案是正確的:) – Lofty

回答

4

這是json_encode的錯。

​​3210:

JSON_UNESCAPED_SLASHES
不要逃避/。自PHP 5.4.0起可用。

所以

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

應該擺脫那些反斜槓。

+0

上帝保佑你,謝謝你。我正在抓我的頭一陣子哈哈。不知道我會很快得到答案。乾杯!我不明白爲什麼我總是會陷入低谷...... – Lofty

+0

@高尚的人通常會對快速修復的問題下決心,因爲它往往表明提問的人在提問之前沒有做足夠的研究。堆棧溢出並不打算作爲修復相對新手錯誤的第一個呼叫點(雖然這正是它慢慢變成的) – Martin

2

更好的格式您直接在查詢日期...

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

here所有格式化選項

+0

這是一個更簡潔的更簡潔的解決方案。 – Martin