2

I'm looking for some code which can check whether 30 days have passed, and if so the script will stop working; if not, it calls the start() function.

I only need to do it in Javascript.

var date = 11;
var month = 10;
var year = 2012;

function Checkday() {
    if ( // check 30 days ) {
        start();
    } else {
        stop();
    };
};

I'm also wondering whether there is a way to add PHP code in a JS file. I know this is a stupid question but I'm just curious.

Thanks in advance.

Edited:

We will be selling the JS file so we will be setting the date in the JS file. So we want it to check the expiry time and stop functioning.

Layke
  • 51,422
  • 11
  • 85
  • 111
deerox
  • 1,045
  • 3
  • 14
  • 28

3 Answers3

4

I've made a fiddle for you.

var date = 10;
var month = 10;
var year = 2012;

var expires = new Date(year, month, date);
function checkDate() {
    var d = new Date();
    return(d>expires);
}

Give your js file the ending .php and put header('content-type: application/x-javascript'); at the beginning of it. Now you can <?php echo $variables; ?> inside of it. This workouround must not work in ie*.

Nevertheless: if you're selling the js and want to stop this function when a given timepoint is reached: This is not solvable via javascript! Anybody could edit a javascript file - like the date, month, year params. Except you hava a js packer, which - IMHO - is not safe enough.

sics
  • 1,298
  • 1
  • 11
  • 24
2

This might help you..

var date = 11;
var month = 10;
var year = 2012

function Checkday() {
    if ( !isExpired() ) {
        start();
        } else {
        stop();
    };
};

function isExpired() {

    var current = new Date(); 
    current = current.getTime(); 

    var dateToCheck = new Date(); 
    dateToCheck.setFullYear(year); 
    dateToCheck.setMonth(month); 
    dateToCheck.setDate(date + 30) 
    var checkdate = dateToCheck.getTime();

    return checkdate < current
}
Chand
  • 91
  • 4
2

Silly question, but what is to stop the people you are selling this JavaScript file to from opening it up themselves and changing the date? Are you selling this file to them so they can use it in their website? I have to imagine that they will be smart enough to think of that.

Also, php is a server-side language while JavaScript is a client-side language, so putting php in the JS file directly wouldn't do you much good. Really your best bet is to save the dateinfo in a cookie and check it before calling the function.

Craine
  • 490
  • 8
  • 17