I want to set a transient, but that transient to expire at midnight.

How can I calculate the time until midnight in PHP for use in that transient definition?

  • Is there a reason you need to be that specific? If you need something to occur at a very specific time I don't think a transient would be appropriate. Jun 20 '18 at 16:06
  • Its a contest. I want to save theres a contest in progress, but that contest has to end to a specific date (given in the wp-admin). So when we are at the day where the contest end, that transient must end at midnight, and not at whenever that transient was set.
    – Fredy31
    Jun 20 '18 at 16:07
  • Like the problem if I was to setup a 24h transient, is if, for some reason, the transient resets a 11PM, i'm stuck with it active until 11PM the day the contest must be gone.
    – Fredy31
    Jun 20 '18 at 16:08
  • that looks like complicated. it would be easier to store the contest end time and to compare it at the current time. and time calculation is not WordPress related, it's PHP strtotime("tomorrow")
    – mmm
    Jun 20 '18 at 16:09
  • A transient is for holding data that expires, so I don't see how that's relevant for this situation, because there isn't any data that needs to expire or be deleted. How were you going to use this transient? Where are you checking it's value/expiration? Jun 20 '18 at 16:09

Do you just want this lines ?

$midnight_time = strtotime('tomorrow') - time();
set_transient( 'transient_name', $transient_datas, $midnight_time);

$midnight_time represent the seconds before midnight happens.

You can ask your server to curl a page with a random parameter if you have cache to be sure your transient is consumed with a cronjob.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.