PHP Task Scheduler Cron Job in PHP
Posted in: PHP

Cron Job in PHP on Localhost in Windows | Scheduler in PHP

Running Cron Job in PHP is very easy at server side and also at localhost. In this tutorial i will show you how you can create Cron Job in PHP on Localhost in Windows. You can also say it as Creating Scheduler in PHP at localhost. We use the Cron Job for scheduling the task for some specific interval. Cron helps us to run our script automatically when the time comes. You can set the time in seconds, minutes, hours, days, weeks, months etc. So lets start

In this tutorial we will create an automatic entry process which will enter some random values in the database table at given interval. We will distribute this tutorial in 3 parts.

  • PHP Script
  • Shell and other
  • Task Scheduler

First of all we will create a script which will save the data in the database whenever it runs. Our database “crontest” table “webentry” will have 3 columns (Id,Name,Time). Lets create the connection class first.

As you can see we have created the connection class. Please update the credentials with yours. Now we will create another class “EntryClass” which will have our functions for entering the values to the database table.

Now you can see that we have created the function which will save the provided values to the database.




For calling this function we will create a php file “autoentry.php” and call the addEntry function with some random values.

Now our second part will start.  But before starting please check that our code is working properly or not.

Create a text file in which we will enter the PHP.exe location, our script location and save it as .bat file it will look like something name it as “scriptrunner.bat”

For running a .bat file we need shell script which will call the autoentry.php page. For doing this we will create a .vbs file and name it as autoentryscript.vbs. You can create it over the notepad and save it as .vbs. Open the vbs file and add these lines to your vbs file.

After making the vbs we have completed our second part. Now we will move to our final part. Please follow the list and checkout the images.

Go to control panel search task in search bar

Under Administrative Tools you can see the schedule task check image.

Administrative tools

Now a new window will open form the menu bar click “Action” Now click “Create task”.

A new window will open. Provide a name whatever you want. Provide a description.

Move to Triggers click on new. Enter details according to your need check the image for ref.

Click ok. Now move to actions tab click on new under action choose start a program. Under settings browse the .vbs file and click ok.
Cron Job in PHP

Now you task is added to the que. If you want to test the task then click run button from right sidebar. It will run immediately. If you want to test the automatic process then close the task scheduler when the time come it will run.

You can also check that your script is working or not by running the .vbs file directly.

Hope you like the tutorial if you have any advice then please comment below.

~ Thank You for Reading Happy Coding ~

Comments (2) on "Cron Job in PHP on Localhost in Windows | Scheduler in PHP"

  1. Hi Vivek,
    thank you very much for this tutorial!

    It works (almost) super good.
    Almost therefore:
    In the scriptrunner.bat the script ‘addentry.php’ must be called in one line with the php.exe.
    Otherwise the php-script is opened with the program (e.g. the PHP-IDE) which is defined in Windows to open PHP-files.

    Correct is:
    “C:\xampp\php\php.exe” -f C:\xampp\pathtoyourproject\addentry.php

Leave a Reply

Your email address will not be published. Required fields are marked *