Convert an int to time()

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 2

  • 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();
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x