0

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?

2
  • This is similar, I wonder whether your question is close enough to be dupe: stackoverflow.com/questions/15877121/… Commented Apr 28, 2016 at 10:01
  • There he not use TIMESTAMPDIFF. Commented Apr 28, 2016 at 10:04

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

You might explain that you have also added an alias to give the result column an easily usable name try this is not helpful to others that may read your answer at a later date
0

you are missing quotes in the query

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.