Good day.
Recently, I had to write a page to send posts on the walls of social networks. The task was to post on the wall at a certain time and not immediately.
I wrote a page with fields for selecting social networks, the text of the message and the field for entering the dispatch time. In addition to the usual calendar with the choice of the date and time of sending, I added another field with the input of the criterion of sending according to the cron rule.
')
Template * * * * *
After that, the question arose - how to get only the necessary fields to send?
Cron is triggered every minute and causes the script to send posts, and every minute looking through the entire table of posts prepared for sending and comparing with the current date / time would be an extra operation.
I use a MongoDB database, which can select all fields by regular expression. Here is what I did:
We take the current date and time and translate into an array
$dateTime = explode(' ', date('i G jn w', time()));
Next, we build a regular expression that selects records that are suitable for sending at the current time (current minute).
$joe_search = new \MongoRegex(
'/^(\*|((\d{1,2}\,)|(\*\/))*('.($dateTime[0]*1).'){1}(\,\d{1,2})*)\s'.
'(\*|((\d{1,2}\,)|(\*\/))*('.($dateTime[1]).'){1}(\,\d{1,2})*)\s'.
'(\*|((\d{1,2}\,)|(\*\/))*('.($dateTime[2]).'){1}(\,\d{1,2})*)\s'.
'(\*|((\d{1,2}\,)|(\*\/))*('.($dateTime[3]).'){1}(\,\d{1,2})*)\s'.
'(\*|(([1-6]{1}\,)|(\*\/))*('.($dateTime[4]).'){1}(\,[1-6]{1})*)$/i'
));
$cursor = $collection->find(array("sendRule" => $joe_search));
sendRule is the name of the field in the database that contains our cron rule.
$ dateTime [0] (minutes) can be with zeros at the beginning (for example, 05 minutes), so we multiply them by 1 to remove zero.
Tested * 1,12,20 7,8,12 * * / 3 (every minute 1.12 and 20 hours 7.8,12 of every month on Wednesday).
That's all, then we cycle the resulting array and send everything we need.