Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I' like to run a cron every 24 hours at midnight PST ( = GMT -8 )

This is what I have

if ( !wp_next_scheduled( 'cron_hook' ) ) {
    //reset on 00:00 PST ( GMT -8 ) == GMT +16
    $timeoffset = strtotime('midnight')+((24-8)*HOUR_IN_SECONDS);
    if($timeoffset < time()) $timeoffset+(24*HOUR_IN_SECONDS);
    wp_schedule_event($timeoffset, 'daily', 'cron_hook');
}

This sets a daily cron on midnight GMT -8 (24-8) and postpone it 24 hours if it's already in the past so the cron doesn't get triggered at the time of creating.

Am I correct with this approach or do I miss something?

I've already tested it but since my server is running with GMT + 0 I cant verify that for other timezone

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Almost,

WP Cron jobs do not run at specific times, they are approximate, and all timestamps should be UTC, as WordPress always deals in UTC timestamps. If you want midnight PST, you'll want to specify 8PM UTC.

Also for example, your above code suggests midnight PST, but it may not run at midnight PST. If nobody visits the site at the specified time, and there's 4 hours before someone arrives, then the cron job will occur at 4am.

If you're wanting accurate cron jobs that are not approximate, you will need to setup a server cron job to call the wp cron URL at fixed intervals.

I would also choose a more specific name than 'cron_hook' to prevent clashes and issues in the future

You can test this easily by just figuring out what time midnight PST is in UTC, aka 8AM, does your cornjob fire at 8AM UTC?

share|improve this answer
1  
Yeah a knew that cron doesn't trigger at this time but this doesn't matter in this case cause the plugin were I use it needs to run anyway (which triggers the cron). The plugin should reset a counter at this time and the counter only counts if someone hit the site 'cron_hook' is just a the example. I use a prefixed hook name in my case. Thanks Tom, this answered my question! –  revaxarts Jan 25 '13 at 13:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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