I have this code in my custom plugin. It's supposed to check if a timestamp is older than 24 hours

 $today = new DateTime();
 foreach( $customers_to_activate as $customer_details ){
   $time = $customer_details['approved_date']; //this is a timestamp stored into usermeta
   $registration_date = new DateTime("@$time"); //converting the timestamp into a DateTime object
   $interval = $today->diff($registration_date); //checking if the timestamp is older than 24 hours
   if( $interval->days > 1 && empty($customer_details['last_password_reset']) && !(bool)$customer_details['account_details_sent'] || empty($customer_details['account_details_sent']) ){ //code stuff here }

I've noticed that the check will not occur correctly and a test account I've registered today will get an email from my wordpress system before 24hours are elasped. Is there something wrong into the code?

  • can you rephrase what the problem is? You stated it doesn't work but it's unclear which part is not working, it looks like you've done a full end to end user/integration test but haven't isolated the issue any further or provided details of how and when the code is executed. I also see comments describing what the values are supposed to be, but there is no code to check these assumptions and validate they are present and have the expected value types, and no checks on arrays, e.g. $customers_to_activate may be an empty array or null and it's unclear where it comes from.
    – Tom J Nowell
    Nov 6 at 13:58
  • 1
    it's also likely the problem has nothing to do with this specific code, for all we know the email does send but gets binned by the recieving server as junk or fails email security checks such as SPF/DKIM/etc. As an aside, I notice you've asked 46 questions but have only accepted answers on 6 of them, this isn't a good indicator for people interested in writing answers, I'd consider returning to old questions and either writing up the solutions you found, accepting appropriate answers, or making sure the people who did answer understand why their answer can't be accepted.
    – Tom J Nowell
    Nov 6 at 14:01
  • The emails will be sent correctly but not after one day as I want. The array will be populated after a check into the database that will consider some values to decide if an user account needs to be activated or not. I've missed that part because it will work as expected. To be clear what is causing problem is the interval check between the two timestamp, I will need to wait for 24 hours before an email will be sent basing this action on the timestamp
    – ICTDEV
    Nov 6 at 14:52


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.