-4

I'm trying to convert SQL datetime to a Y/m/d G:i:s format. It has formated, but there is an extra backslash in front of every frontslash... I've tried str_replace and stripslashes and non of them have worked...

Data: http://www.zewde.org/instagram/script_new/data.php

Code:

<?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);
?>
tkausl
  • 13,686
  • 2
  • 33
  • 50
Lofty
  • 5
  • 2
  • You're outputting JSON. `\/` is how you express `/` in a JSON string. There is no ``\`` in the data, that's just the encoding. – Quentin Sep 06 '16 at 21:50
  • And on stackoverflow, editing it doesn't help you, it's still there. Ask a mod to delete the question completely and change your password! – baao Sep 06 '16 at 21:50
  • Thanks for the help, tkausl's answer is the right one :) – Lofty Sep 06 '16 at 21:57
  • 1
    Also please reseach `MySQLi` or `PDO` as MySQL_ is now ***deprecated*** and is not safe in general or even available in PHP7. Please [translate all your MySQL_ code to `MySQLi_`](http://www.phpclasses.org/blog/package/9199/post/3-Smoothly-Migrate-your-PHP-Code-using-the-Old-MySQL-extension-to-MySQLi.html) at the very least. – Martin Sep 06 '16 at 21:59

2 Answers2

4

This is json_encodes fault.

json_encode() options:

JSON_UNESCAPED_SLASHES
Don't escape /. Available since PHP 5.4.0.

So

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

should get rid of those backslashes.

tkausl
  • 13,686
  • 2
  • 33
  • 50
  • God bless you and thank you. I was scratching my head for a while lol. Didn't know I would get the answer this quickly. Cheers! I don't understand why I always get downvoted though .... – Lofty Sep 06 '16 at 21:55
  • @Lofty people will generally downvote questions that are quick fixes because it tends to show that the person asking the question hasn't done enough research before asking the Question. Stack Overflow is not intended as a first point of call for fixing relatively novice bugs (although that's what it's slowly becoming) – Martin Sep 06 '16 at 22:01
2

Better format your date directly in the query ...

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

See here for all formating options

baao
  • 71,625
  • 17
  • 143
  • 203