The scenario is to run insert_post function every midnight (00:00) of local time. Must run daily on weekday.

function add_daily_task(){
    if((date('l', time()) != 'Saturday') || (date('l', time()) != 'Sunday')){ 
        // insert post

if(!wp_next_scheduled( 'add_daily_task_schedule')){
    wp_schedule_event(time(), 'daily', 'add_daily_task_schedule');
    // how should change time() to meet my local schedule?

add_action('add_daily_task_schedule', 'add_daily_task'); 
  • The goal of this cron job is to grab financial data and import it to custom table. This data will be read with an interactive chart. The financial data only run on weekday. Aug 4 '13 at 7:30

Not sure what is the question, but the use of time() is wrong as it will give you at best the local time of the server and not the local time of the site which takes into consideration the time zone configuration of WordPress.

The function that you should use is current_time.

  • Yes, the current_time is correct for the condition Thanks! Oct 24 '21 at 3:35

wp_schedule_event can not be used for PRECISE time measurements as you are trying to do. An event scheduled with wp_schedule_event rarely runs exactly when it is scheduled to run, it runs sometime after the time which it has been configured for.

To understand this, you must understand that php doesn't know what time it is until you ask it to look. Wordpress checks the time whenever someone visits your website. It then compares the current time with the time of any scheduled tasks. If the time is equal to or greater (after) than the scheduled time, it runs the task.

So this means that even if you figure out your time scheduling issue, your task will not run until both the time you choose has elapsed AND someone loads your website.

With all of that said, if you still want to try and make this work, here is one possible solution. Add a new interval to your functions.php

function cron_add_weekly( $schedules ) {
    // Adds once weekly to the existing schedules.
    $schedules['weekly'] = array(
            'interval' => 604800,
            'display' => __( 'Once Weekly' )
    return $schedules;

change your function to wp_schedule_event(time(), 'weekly', 'add_daily_task_schedule');

If you put this in place at exactly midnight next saturday, Wordpress will attempt to run this schedule every week at the same time of day (assuming someone goes to your site to trigger the schedule).

  • 1
    Looking back at this answer, I feel I should also mention that if you are looking for precisely timed cron jobs, you can disable the WP_Cron, and setup a "real" cron on your server (so long as your hosting provider gives you this ability).
    – Matt Keys
    May 14 '14 at 20:50
  • Hi @Matt Thanks for the answer, assume we can disable wp_cron, how we can assure if any cron schedule not running at the same time (server load). Please share your experience e.g. I need to make sure that cron triggered not overlapping other cron jobs running. Sep 6 '20 at 9:16
  • 1
    From my reading, it looks like WordPress puts a 'lock' in place while a cron is running, and won't start a new cron until there is no lock. This is done using a wp transient called "doing_cron", you can see the code in the spawn_cron function in /wp-includes/cron.php.
    – Matt Keys
    Sep 8 '20 at 21:24
  • Thanks, @Matt a very good explanation. Sep 9 '20 at 20:13

First, make sure your WordPress date and time settings are correct as well as your local server time.

Then use this code:

$datetime = new DateTime('midnight', new DateTimeZone(get_option('timezone_string'))); 
$datetime->setTimezone(new DateTimeZone('UTC')); 
$timestamp = $datetime->getTimestamp();

wp_schedule_event($timestamp, 'daily', 'your_action_hook_name');

You can modify 'midnight' to 'tomorrow 3am' or whatever suits your needs.

If you need your task (inserting posts or whatever) to run only on weekdays, you can implement that logic within your hook function.

Refer to the wp_schedule_event() code reference for recurrence options e.g. 'hourly', 'daily', 'twicedaily', 'weekly'. These are all built-in options you don't need to define using the cron_schedules filter.

The WP Crontrol plugin is useful for making sure your cron events are set up correctly.

New contributor
Tyler is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

The function:


gives the UNIX timestamp in the GMT timezone.

So if you are GMT+2 you can use:

time() + 2*60*60;

or if you are GMT-4 you can use:

time() - 4*60*60;

Try replacing time() with mytime() in your code, where:

function mytime(){
    $gmt_offset_in_hours=-2; // for GMT-2 (EDIT this value) 
    return time() + $gmt_offset_in_hours*60*60;     
  • Or even use 2 * HOUR_IN_SECONDS! The WordPress time constants are great for readability.
    – mrwweb
    May 11 '15 at 18:07
  • 1
    @mrwweb yes I agree these constants can be handy. Looking at my answer now, I think we might instead try current_time( 'timestamp' ) for the local time ;-)
    – birgire
    May 11 '15 at 18:20

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.