how to automatically change a value using date and time

Discussion in 'PHP' started by stefffff, Jan 12, 2009.

  1. #1
    Hello Php masters... i have a question for you: what is the easiest way to update a value after a certain day comes.

    Let me get more specific:
    i have a var say: $var=1; and let's say today is Monday
    i want to change the $var=0; when the day is Wednesday
    how can i achieve that? any ideas?

    i need that to be automatically done, and not by using javascript, but php & mysql , as that value is actually located inside a Mysql table... i've seen this working on CMS sites, like joomla, where the user can specify the date when an article will become published, without having to manually change that date... i hope you guys can help me... i'm running out of ideas... and google can't help me either... i can't find anything related:(
     
    stefffff, Jan 12, 2009 IP
  2. MyFlash13

    MyFlash13 Peon

    Messages:
    137
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might want to use a cron job. A cron job is a script on the server that runs every 5 minutes, or 2 days or how often you want it to run.

    So you would probably want to write a script to change your var, then set it to run every day on your server.

    How to set up a cron job (linux servers): http://www.google.ca/search?q=cron+jobs
    How to set up a scheduled task (windows servers): http://support.microsoft.com/kb/814596
     
    MyFlash13, Jan 12, 2009 IP
  3. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can do it directly in PHP too.

    
    if (date('D') == 'Wed') {
    	$var = 0;
    }
    else {
    	$var = 1;
    }
    PHP:
    or more compact if you have only two values

    
    $var = (date('D') == 'Wed')? 0: 1;
    PHP:
     
    french-webbie, Jan 12, 2009 IP
  4. stefffff

    stefffff Active Member

    Messages:
    194
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    i think the cron job is a bit too much... like i've said,.. joomla already has something similar to what i want...

    cms systems use it for quite a while.

    in joomla when i write an article i can choose when to appear on my site.. i doubt there is a cron script checking every second... as for the french-webbie, that's hardly what i was looking... my variable has a value witch is stored into a database table.. i'm sure there is a way... i just need to find a proper example...hmmmm! any other ideas? thanks in advance!
     
    stefffff, Jan 12, 2009 IP
  5. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well, you can populate an array from the different values you need before doing the date code, or write an SQL query taking the date into account.
     
    french-webbie, Jan 12, 2009 IP
  6. stefffff

    stefffff Active Member

    Messages:
    194
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    can u be more specific? perhaps an example? that's probably the best way to understand this...
     
    stefffff, Jan 12, 2009 IP
  7. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So you have a list of values for a variable stored in DB. Do you have a day/date/datetime field, or how do you link a specific day with the value you want?
    I guess it's you turn to be more specific ;)
    (you can reply by PM though)
     
    french-webbie, Jan 12, 2009 IP
  8. artflutter

    artflutter Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    Wrong, all popular CMS' use a cron job to handle their scheduled posts. There are some variations upon this though, such as inserting a snippet of code in your footer that is then run every time someone visits your site (basically a web cron). You can use this to get the server datetime and progress accordingly. Trust me using a cron job is the best option and you can set them up from within your sites cpanel so you don't even need to know any unix commands.
     
    artflutter, Jan 13, 2009 IP
  9. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ARTFLUTTER is correct they do use a cronjob of some sort!
     
    atlantaazfinest, Jan 13, 2009 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Whoever wrote a CMS like that should be shot.

    The correct way to do this would be insert it into your posts(/whatever) database as per default, but set a future timestamp. (for whichever field you use to say when it's visible.)

    When you do your query, only get posts where the timestamp is less than the current time() value, if it's greater do not access it yet.

    No cron jobs required at all for that.

    Dan.
     
    Danltn, Jan 13, 2009 IP