The database stores the date in timestamp format. I need to take from this date only hours: minutes: seconds and then present this data in seconds for the json object. I stopped at the fact that now there are string hours: minutes: seconds (in the screenshot), how to translate them into seconds?

enter image description here

<?php $con = mysql_connect("localhost","root","root"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("triger", $con); $data = mysql_query("SELECT time(`time`) AS ts, value FROM triger1") or die ("Query error"); header("Content-type: text/json"); $V0 = array(); while($row = mysql_fetch_array($data)) { $V0[]= array( $row['ts'], (int) $row['value']); } mysql_close($con); echo json_encode( array($V0), JSON_NUMERIC_CHECK); ?> 

    2 answers 2

    Use the mysql function TIME_TO_SEC ()

     SELECT TIME_TO_SEC(TIME('2016-06-21 01:02:06')); // Вернет '3726' 
    • Damn, I remember trying it, but it didn't work, but now it works, thanks a lot! - D. Sakulin
    • @ D.Sakulin mark the answer as correct - Naumov

    Like this:

     <?php $con = mysql_connect("localhost","root","root"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("triger", $con); $data = mysql_query("SELECT time(`time`) AS ts, value FROM triger1") or die ("Query error"); header("Content-type: text/json"); $V0 = array(); while($row = mysql_fetch_array($data)) { $V0[]= array(strtotime($ts), (int) $row['value']); } mysql_close($con); echo json_encode( array($V0), JSON_NUMERIC_CHECK); ?> 

    PS Parsing a string with a time on the symbol: and take the very last element - seconds

    • Thanks, I will know that through explode it is possible. - D. Sakulin
    • Don't forget to mark the answer with the right one that suits you the most so as not to confuse others :) - ikerya
    • Judging by the question, you still need the number of seconds, and not the rest of those seconds. - Naumov
    • Do you need the number of seconds of the string type 10:21:56 - 37316? - ikerya
    • @ D.Sakulin Pay attention to the message above - Naumov