Cron - Date Format (Last & Current) to compare.

Discussion in 'PHP' started by wnetwork, Aug 27, 2010.

  1. #1
    I have an Advertising PHP Script and I recently added a new option Per Day along with Per Impression and Per Click.

    I'm coding a Cron.php file to update Per Day ads daily (once a day or 24 hrs) by either Automatic Cronjob or manually by loading the page.
    I have no problem loading the page and as far I can tell, it seem to works fine.

    However, I realized what happen if the server malfunctioned (corrupted) or a bad person find the cron.php file and accessed the page multiples times within a Day or 24 hours. The cron.php will process every times which is not a good thing.

    Now, I thought of a solution- added a time/date function comparing Last Date Cronjob and Current Date.

    If the current date is less than a Day or 24 Hours, then cron.php will do nothing.
    If the current date is over a Day or 24 Hours, then cron.php will do the works. After the works done, Iit will also get the new current date and update the Last Cron field in the TABLE row.


    $today = date("F j, Y, g:i a"); // August 27, 2010, 5:59 pm
    
    if ($today > $conf['lastcron']) // make sure today date is current (over a day/24 hrs) than last cron date
    {
      $sql = mysql_query("UPDATE tablename1 SET credits = credits - 1 WHERE type = 'CPD' AND unlimited = 'no'") or die(mysql_error());
      $sql2 = mysql_query("UPDATE tablename2 SET lastcron = '$today' WHERE id = 1") or die(mysql_error());
    } 
    else 
    {
    echo 'Too early to do cronjob. Must be every 24 Hours / 1 Day. <br />';
    echo "Time Now: ".$today."<br />";
    echo "Last Cron: ".$conf['lastcron'];
    }
    PHP:
    Is what I have done make sense or wrong?



    if ($today > $conf['lastcron']) // make sure today date is current (over a day/24 hrs)
    PHP:
    Another question- will it do what it suppose to do?



    Forgive me, I usually know what I'm doing but currently I'm preoccupied with some major events coming up which is distracting my programming mind.
     
    Last edited: Aug 27, 2010
    wnetwork, Aug 27, 2010 IP
  2. wnetwork

    wnetwork Greenhorn

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Ok- I noticed there is a problem with the date comparing. When the Current Date goes past the Last Date, I'm able to get the Cron going. Great... but when reloading the page again, it show the cron message again and again. So it not stopping me from reloading the page and process the Cron job meaning credits continue to go down (-1 each page load).

    Right now, I'm not understanding what is wrong. I would think the page will stop loading the Cron until the next day.
     
    wnetwork, Aug 27, 2010 IP
  3. wnetwork

    wnetwork Greenhorn

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Alright- I think I figured it out since it seem to be working as it supposed to do.

    All I did was just create a new php file and start coding as new again. Started small then work my way up. I changed the date format.
     
    wnetwork, Aug 29, 2010 IP