Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7819591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:00:38+00:00 2026-06-02T07:00:38+00:00

I’m developing a web based app that checks for updates on the server and

  • 0

I’m developing a web based app that checks for updates on the server and notifies the user if there are any. This is an alternative to the push method; the background process in the app checks the PHP file every hour to see if there are new additions to the MySQL database. If there are it notifies the user on the home screen.

The part I am having trouble is comparing timestamps. I keep the "lastUpdate" variable which tells the last time the phone accessed the PHP file in local storage in the JavaScript file. Every time the phone connects it passes the variable to when it last connected, if there are any new entries in the database with timestamps greater than that of the last update the notification count will be increased.

PHP:

$lastUpdate = $_GET["q"];
//connect to server

mysql_select_db("dbr", $con);
$sql="SELECT * FROM notification";
$result = mysql_query($sql);

$updateCount = 0;
if ($lastUpdate == "1970-01-01 01:00:00")
{  $compareTime = time();}
else
{  $compareTime = intval($lastUpdate); }

while($row = mysql_fetch_array($result))
{
    $rowTime = strtotime($row['timeAdded']);
    if ($rowTime >= $compareTime) 
    {   
        $updateCount++;
    }
}
echo json_encode( array('lastUpdate' => $compareTime, 'numUpdates' => $updateCount,     'passedValue' => $compareTime) );

JavaScript:

var timedLoop;
var timer_is_on=0;
localStorage.lastUpdate = "1970-01-01 01:00:00";
localStorage.numUpdates = 0;
localStorage.passedValue = ""; //used it for debugging

function timedCount()
{
    document.getElementById('testContent').innerHTML = "lastUpdate: " + localStorage.lastUpdate;
    document.getElementById('testContent2').innerHTML = "numUpdates: " + localStorage.numUpdates;
    document.getElementById('testContent3').innerHTML = "passedValue: " + localStorage.passedValue;

    contactServer(localStorage.lastUpdate);
    if(Number(localStorage.numUpdates)>0)
    {       
        alert("NOTIFICATIONS AVAILABLE");
        notifyUser();
        localStorage.numUpdates = 0;
    }
    document.getElementById('txt').value=new Date();
    timedLoop=setTimeout("timedCount()",10000);
}

function contactServer(update)
{
    if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest();}
    else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var result = JSON.parse(xmlhttp.responseText);
            localStorage["lastUpdate"] = result["lastUpdate"];
            localStorage["numUpdates"] = result["numUpdates"];
            localStorage["passedValue"] = result["passedValue"];
        }
    }
    xmlhttp.open("GET","address.php?q="+update,true);
    xmlhttp.send();
}

Problem is it is not working for me. I’m fairly new to this so I’m not sure if I’m doing it correctly. The function timedCount() is loaded in the body by “onload” so it runs at the start and keeps running every 10 seconds in this case. But the timestamp values stop changing after the first two loops. If I try to change the code a bit I get completely unexpected results. I’ve been at this for a while so any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:00:40+00:00Added an answer on June 2, 2026 at 7:00 am

    1) You start with localStorage.lastUpdate = "1970-01-01 01:00:00"; and send that value to the server as a GET parameter named q.

    2) The first time this matches your if statement and $compareTime = time(); is executed. This value, the current timestamp, is the returned to the JavaScript as the lastUpdate JSON property.

    3) In JavaScript you pick this value up and run localStorage["lastUpdate"] = result["lastUpdate"]; to save it. Ten seconds passes and you make another request, now with the previous timestamp as the q parameter.

    4) Your PHP’s else statments kicks in and assigns the given — previous — timestamp to the `$compareTime variable. Thus you use the very same data once again for the following while loop.

    Simply returning 'lastUpdate' => time() instead in that json_encode() would do the trick. Else it would continue to request the same time over and over.

    However I am curious about how you mix the formats. time() gives an actual timestmap like 1335048166, which is a totally different format from “1970-01-01 01:00:00”. If you have timestamps also in the database I suggest sticking with that one format throughout your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.