Creating a Scheduled Event
First we’ll create a function that will schedule our event. Mine is called “mycronjob” and it will run once every day. All this code can go into your plugin’s main file, outside the main function:
// create a scheduled event (if it does not exist already)
function cronstarter_activation() {
if( !wp_next_scheduled( 'mycronjob' ) ) {
wp_schedule_event( time(), 'daily', 'mycronjob' );
}
}
// and make sure it's called whenever WordPress loads
add_action('wp', 'cronstarter_activation');
Adding your Repeat Function
this is just a placeholder for your own code you’d like to run on a recurring basis:
// here's the function we'd like to call with our cron job
function my_repeat_function() {
// do here what needs to be done automatically as per your schedule
// in this example we're sending an email
// components for our email
$recepients="[email protected]";
$subject="Hello from your Cron Job";
$message="This is a test mail sent by WordPress automatically as per your schedule.";
// let's send it
mail($recepients, $subject, $message);
}
// hook that function onto our scheduled event:
add_action ('mycronjob', 'my_repeat_function');
After all that if you face some problem with specific time then you can check this answer Here