I have got around 10 <div>'s and in every <div> there is a different datetime. I want to call a java-script function when the clients local time matches the time in that <div>'s.

//sample scenario

<div id="a" class ="help">2/3/2013 6:39:14 PM</div>
<div id="b" class ="help">2/3/2013 2:39:14 PM</div>
<div id="c" class ="help">2/4/2013 6:39:14 PM</div>
<div id="d" class ="help">12/29/2013 10:39:14 AM</div>

if the current date-time of the client machine matches the date-time in the <div> ; call a function say callMe()

for now I have got only - some php code :

// this is the div where the time is inserted.

<div id='<?php echo $needAnswer[$i]; ?>' class='help'></div>

// The div id and the unix time is taken from the database.

echo "<script type='text/javascript'>putTime(".$questionData['expiry'].",".$needAnswer[$i].")</script>";

And the javascript :

// this function converts the stored unix time to local time and inserts in the html (x is the unix time , y is the div id)

function putTime(x,y) {
var theDate = new Date(x*1000); // convert time value to milliseconds
var dateString = theDate.toLocaleString();
document.getElementById(y).innerHTML = dateString;
}

This all I have.

Please help me solve this step by step as I am not too good in javascript.

Member Avatar for LastMitch

Please help me solve this step by step as I am not too good in javascript.

Oh, my goodness. Why are you using javascript when you can just used PHP/MYSQL?

You also need to fetch the column that has the current date & time but I don't see that query connecting to your javascript?

Here is how you do it:

function putTime(x,y) {

    var theDate = new Date(x*1000); // convert time value to milliseconds
    var now = new Date(); // Now

    var dif = theDate.getTime() - now.getTime(); // Difference between dates in millisseconds

    // Date is in the past
    if ( dif < 0 )  {
        // Do Something     
    }
    // Date is in the future
    else {
        setTimeout(function() {
            // Code to be executed when the 'now' becames 'theDate'
        }, dif);
    }
}
commented: Nice! +9

scenario :
few date-time (unix time) is coming from the database and I have to print these datetime on the clients web page. But the screwed up part is ; The time coming from the database is in unix time , and it should be echo'ed on the client system according to his local time (which means I have to tak into consider the Day light saving and al the time offeset issues) . So to solve this what I did is sending the value to a javascript (so that it wil take the local date() ) which then multiplies with a 1000 and puts on the clients screen. I have done till here. ( The above code what I wrote ) .
?? What I want to do now is , I want to a call a javascript function when the date-time inside these <div> hits the local time (i,e. when the clients local time matches any of the time in the div ; a function is called).

Imagin you have some expiry-time in seperate div's (the above code-- all that div). I need help in coding a function which loops through all this div (grabbing the expiry time - date-time and the div id). Also this function should be able to keep track of the local time (as the current local time changes every second) , if any of the div's expiry time matches the current local time .. an alert is given)

Member Avatar for LastMitch

@ckjaseem

What I want to do now is , I want to a call a javascript function when the date-time inside these <div> hits the local time (i,e. when the clients local time matches any of the time in the div ; a function is called).

I think AleMonteiro gave you a outline how to get the date-time in your div with the example he just posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.