Trigger a cron every 24h GMT -8

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

This is what I have

Read More
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

Related posts

Leave a Reply

1 comment

  1. 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?