0

I'm currently developing a plugin which uses WP cron. During the installation of the plugin, I'm creating 2 needed cron jobs with a timestamp of midnight -2 and a and midnight -1 hour, since job 2 should only run 1 hour after job 1.

Even when using a timestamp during creation which is 3-4 hours in the future, it seems like that both functions are getting initially executed directly after the plugin install / update, which results in a lot of issues.

wp_schedule_event( strtotime( 'midnight' ) + ( - 2 * HOUR_IN_SECONDS ), 'daily', 'test1' );

wp_schedule_event( strtotime( 'midnight' ) + ( - 1 * HOUR_IN_SECONDS ), 'daily', 'test2' );

Any idea how to prevent an initial execution of each event during creation?

1
  • This shouldn't happen due to cron. Are you sure you are not firing those functions elsewhere?
    – vancoder
    Jul 19, 2022 at 20:53

1 Answer 1

1

strtotime( 'midnight' ) will give you midnight for today, which has already happened! You want midnight for tomorrow.

e.g. date( 'F jS H:i:s', strtotime( 'midnight' ) ) on July 19th is July 19th 00:00:00

However, date( 'F jS H:i:s', strtotime( 'tomorrow midnight' ) ) would be July 20th 00:00:00

...which is what you want! So just use tomorrow midnight instead.

1
  • Makes totally sense. Thanks for this hint!
    – Johnny97
    Jul 20, 2022 at 11:12

Your Answer

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

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