skip to Main Content

I want to send a reminder email “x” days after an initial email was send.

The admin can choose after how many days they want to send a reminder (after the date of the initial mail).

This amount is stored in this function and returns an integer (for example 14)

public function getAmountDayReminder(): int
{
    return get_field('send_reminder_after_amount_day', $this->getId());
}

The date/time of that the initial email was send is stored in this function

public function setWasNotified()
{
    update_post_meta($this->getId(), self::META_WAS_NOTIFIED, time());
}

Now I want to write an if statement so that the reminder will be send out “x” days after the date of the initial mail was send.

I guess it will look something like this:

if(!$entry->wasNotified() && !$entry->wasReminded()) {
    $remindDate = $entry->setwasNotified() + $this->getAmountDayReminder();
    if ($remindDate === //current time) {
        $entry->remind();
    }
}

This won’t work because I have to covert the output of setwasNotified() and getAmountDayReminder() so that I can add them.

Is this a good solution and how would I execute this exactly?

2

Answers


    • Convert The previous date into time stamp , call it $lastNotified
    • Convert days into timestamp after multiplying (24x60x60x$numberofdays_after) Seconds: called it $daysAfter
    • Add $lastNotified and $daysAfter
    • now compare the recent timestamp time() === ($lastNotified+$daysAfter)
    • if current time is equals to the ($lastNotified+$daysAfter) send New Notification and Save current time stamp as $lastNotified

    For your code:

    if(!$entry->wasNotified() && !$entry->wasReminded()) {
        $remindDate = $entry->setwasNotified() + (24*60*60*$this->getAmountDayReminder());
        if ($remindDate === time()) {
            $entry->remind();
        }
    }
    
    Login or Signup to reply.
  1. I would use the DateTime modify to add getAmountDayReminder() to the time where the first notification has been sent and then compare this to the current time.

    Something like that:

    <?php
    $reminded = DateTime::createFromFormat( 'U', $entry->setwasNotified());
    
    $now = new DateTime();
    
    if ($now > $reminded->modify($this->getAmountDayReminder() .' day')) {
        print('Need to send reminder');
    }
    else {
        print('Do not send reminder');
    }
    

    Do not forget to store whether you already sent the reminder.
    And doing such things in a cron job is probably a good idea.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search