Jump to content

Run Function on Specific Date


Recommended Posts

I'm developing a website for specific EVENTS. Each EVENT lasts for three months and has fixed start date and end date, and they repeat each year: 

  • Event 1: Jan. 1st – Mar. 31st
  • Event 2: Apr. 1st – Jun. 30th
  • Event 3: Jul. 1st – Sep. 30th
  • Event 4: Oct. 1st – Dec. 31st

 I have two functions

  • FUNCTION 1:  should run 3 days before start of the event.
  • FUNCTION 2: should run 1 day before start of last month of the event.

Functions should execute only that specific event and not other future or past events. 

Here how the functions should run for each Event: 

  • Event 1: Jan. 1st – Mar. 31st
    • Function 1: on Dec 29
    • Function 2: last day of Feb
  • Event 2: Apr. 1st – Jun. 30th
    • Function 1: on Mar 29
    • Function 2: last day of May
  • Event 3: Jul. 1st – Sep. 30th
    • Function 1: on Jun 28th
    • Function 2: Last day of Aug
  • Event 4: Oct. 1st – Dec. 31st
    • Function 1: on Sept 28th
    • Function 2: last day of Nov

What I'm doing right now is checking today's date and comparing it to the event date.

  • If event start date is > today and event start date < 3 then run function one.

My problem/questions: 

  1. As of now, I have to remember to execute the functions manually on a specific date. How can I make this automated? Can I execute the function using server Cron job? Will that be a good practice? 
  2. How would you approach in checking the event date to today? I feel there maybe better way of checking the date to run the function instead of what I'm doing. 

I hope my question is not vague and makes sense.

Thank you. 

Link to comment
Share on other sites

7 minutes ago, Barand said:

Seems like you want eight cron jobs (four for each job) than run on the same date every year.

Correct.  Is that good practice? 
Will I be able to secure the function? Only to be run by the cron on my server and admin only? 

Edited by doc1355
Link to comment
Share on other sites

The script is designed so that it can create hundreds of posts at a specific time. Each post is based on a specific criteria, specific custom taxonomy. I don't think WP can do this. Also the script updates the posts based on some other criteria at a certain time. 

Link to comment
Share on other sites

Rather than a number of cron jobs, I would suggest you might want to look at the "At" command in linux.  At is a lot like a cron job, however, the jobs are run once from a queue and then removed.  You could have one cron job that figures out what jobs need to be queued up with At, and just track and semaphore the At jobs in your php code, using a table to track that they were queued.  That allows you to keep a lot of the scheduling in your php code, possibly with some database configuration to give you flexibility.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.