简体   繁体   中英

SQL query TIMESTAMPDIFF with php not working

I try to take a date and do countdown untill that day like following:

The date i want to countdown to: 2016-05-1 00:00:00

Then i want to calculate the diff between the date and now so i can do countdown timer.

I have this:

date_default_timezone_set("Asia/Jerusalem");

if ($result = $db->query("SELECT TIMESTAMPDIFF(SECOND, NOW(), 2016-05-1 00:00:00)")) {

    while($row = $result->fetch_array()) {
        $currentTimeLeft= $row['TIMESTAMPDIFF(SECOND, NOW(), 2016-05-1 00:00:00)']; 
    }

    echo json_encode($currentTimeLeft);
}

I dont understand why this returning empty. What is wrong here?

I see quote issue in your query

try this

if($result = $db->query("SELECT TIMESTAMPDIFF(SECOND,NOW(),'2016-05-1 00:00:00') as datediff")) {

        while($row = $result->fetch_array()) {
                $currentTime = $row['datediff']; 
        }

 echo json_encode($currentTime);

I have added an alias "datediff" to the result column as well

您在查询中缺少引号

if ($result = $db->query("SELECT TIMESTAMPDIFF(SECOND, NOW(), '2016-05-1 00:00:00') as t")) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM