4

I'm not well verse with WP Cron, but I used them in one of my site knowledgeably. I set a custom query for every hour:

add_action( 'init', function () {
    if( ! wp_next_scheduled( 'expire_cpt' ) ) {
        wp_schedule_event( time(), 'hourly', 'expire_cpt' );
    }
    add_action( 'expire_cpt', 'pre_expire_the_offers' );
});
function pre_expire_the_offers() {
 //...
}

Now our procedure to the function changed, and we thought to go for a daily schedule. So I set up the things this way:

add_action( 'init', function () {

    //Used once to delete all the schedule
    //wp_clear_scheduled_hook('expire_cpt');

    //I assumed, by clearing, I no-longer need this
    //$timestamp = wp_next_scheduled( 'expire_cpt' );
    //wp_unschedule_event( $timestamp, 'expire_cpt' );

    if( ! wp_next_scheduled( 'expire_cpt' ) ) {
        //Need to run this at 24:00:00 everyday, not 24 hours from now
        wp_schedule_event( time('00:00:00'), 'daily', 'expire_cpt' );
    }
    add_action( 'expire_cpt', 'pre_expire_the_offers' );
});

I'm doing this on local WAMP server in a Windows Machine. My Local PC time, as I got, echoing echo date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), is 2016-04-11 17:18:58.

A six hour difference can be the GMT and my Time Zone (+6) - that's reasonable.

But the problem is with the Next Scheduled Time.

I'm using:

for debugging Cron jobs.

Debug Bar Cron is showing the current time is: 11:52:54, while my timestamp is showing: 17:52:52. Problem is, the Next Occurrence time is not 00:00:00, but

2016-04-12 11:33:56
1460460836
24 hours

ACM is showing:

In 24 hours
12.04.2016 17:33:56

How can I re-schedule my query/function on a specific time, everyday (editing my current schedule)?

11

time() only returns current time, it doesn't accept any inputs.

$time = time(); // works out to 2016-04-11T12:11:34+00:00

What you want is midnight tomorrow:

$tomorrow = strtotime( 'tomorrow' ); // works out to 2016-04-12T00:00:00+00:00

Note that these are PHP functions and they ignore WP timezone, since it resets PHP time zone to UTC. So if using these you are setting it to midnight in UTC time zone.

To get it right in WP is a mess (this might not work under some configurations that have no timezone_string set, see my post on DateTime in WP for more details):

$date = new DateTime( 'tomorrow', new DateTimeZone( get_option( 'timezone_string' ) ) );
// works out to 2016-04-12T00:00:00+03:00
$timestamp = $date->getTimestamp();

Note: WP Cron isn't guaranteed to run at precise time since it is trigerred by visits to the site. I am not confident if recurrent runs will "stick" at midnight or will slowly slip from there, you might need to readjust periodically.

  • Thanks for the insights as always - the blog post for the enlightenment. Just a simple query: WP Cron triggered on first site visit, that's okay for me. But does that mean, on every late occurrence, even the PHP DateTime ($date here) will/may also slip day by day? – Mayeenul Islam Apr 12 '16 at 4:41
  • As per answer I am not sure about slipping, would need to go through all the related code to figure out, didn't have the resolve to when writing the answer. :) – Rarst Apr 12 '16 at 7:06
  • 1
    Don't worry, kudos to the answer. I've tested it in a minimal and may be unscientific way. I've changed my PC date&time to yesterday. Cleared the schedule. Add the schedule hourly instead of daily [with your PHP DateTime code]. And then changed the PC date&time back to today. Then observing the recurrences (by loading site) on irregular intervals. The timestamp of next execution always stays same like: 11:00:00, 12:00:00, 13:00:00, 14:00:00, 15:00:00, ... – Mayeenul Islam Apr 12 '16 at 8:17

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.