D
D
DarkKefir2018-12-14 00:29:11
PHP
DarkKefir, 2018-12-14 00:29:11

Running cron with conditions?

PHP:

// время создания сущности в UNIX-формате (напр., 150123456)
$time_created = $entity->time_created;

// сегодня
$today = time();

// выясняю день/месяц/год создания сущности
$d = date('j',$time_created);
$m = date('n',$time_created); 
$y = date('Y',$time_created);
      
// указываю течение 3-го, 6-го и 7-го дня с момента создания сущности
$three_days = mktime(0, 0, 0, $m, $d+3, $y);
$six_days = mktime(0, 0, 0, $m, $d+6, $y);
$seven_days = mktime(0, 0, 0, $m, $d+7, $y);

Further in the code, I set tasks depending on what day it is today, when the entity was created, for example:
if($today == $three_days) {
  // тут логика
}

if($today == $six_days) {
  // тут логика
}

if($today == $seven_days) {
  // тут логика
}

Let's say these are notifications sent by email.
I run cron every day to perform this task, for example. at 0-01.
Doesn't always work.
I bet every hour. Same problem.
I suspect that the error is in specifying the periods, i.e. you need an exact match mktime'ov.
So:
if($today >= $three_days) {
  // тут логика
}

due to a number of reasons it is not possible.
I myself have never been a pro, but just learning, so I ask you to help with the decision or give the right advice.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Syomov, 2018-12-14
@DarkKefir

Your comparison is not correct. The date should not be equal to your $three_days, but be between it and 11:59 pm on the same day.
Also, it is better to work with DateTime, where there is DateTime::diff, DateTime::add, etc., it is much more convenient, clearer and more concise than any mktime|date, etc.

S
Stalker_RED, 2018-12-14
@Stalker_RED

if($today == $three_days)
You are expecting a match to the second
, but
And at the same time

mktime(0, 0, 0, $m, $d+3, $y);
i.e. 00:00
I'll be surprised if it ever works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question