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

By | December 9, 2019
PHP Task Scheduler Cron Job 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.

<?php
class connectionClass extends mysqli{
    private $host="localhost",$dbname="crontest" ,$dbpass="",$dbuser="root";
    public $con;
    public function __construct() {
        $this->con=  $this->connect($this->host, $this->dbuser, $this->dbpass, $this->dbname);
        if($this->con){}
        else{
//            echo "<h1>Error while connecting to database</h1>";
        }
    }
}

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.

<?php
require_once ( dirname(  __FILE__). '/../functions/connectionClass.php' );
class EntryClass extends connectionClass{
public function addEntry($name,$time){
        $insert="Insert into webentry (Name,Time) values ('$name','$time')";
        $this->query($insert);
    }
}

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.

<?php
include_once 'EntryClass.php';
$entryClass=new EntryClass();
$name=rand(9999,1111)."Vivek";
$time=date("h:i:s");
$entryClass->addEntry($name,$time);

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”

"C:\xampp\php\php.exe" -f "C:\xampp\pathtoyourproject\addentry.php"

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.

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\pathtoyourbatfile\scriptrunner.bat" & Chr(34), 0
Set WinScriptHost = Nothing

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 ~

8 thoughts on “Cron Job in PHP on Localhost in Windows | Scheduler in PHP

  1. Chetan

    i have created everything but the php file is not getting executed….

    Reply
  2. Tim Onomanide

    Hello, thanks for a nice tutorial. Please, this same scheduled process for emailing report from mysql database in a local Xampp Server to external email address at scheduled time

    Reply
    1. Vivek Moyal Post author

      For doing this you have to create a working script of sending emails. If your script will work then this scheduler will also work.

      Reply
  3. guenter

    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

    Reply
    1. mohamad

      it happens with me, instead of executing. it opens IDE. guenter your suggestion works with me

      Reply
    2. Enock Lalusya

      This does not work for me please someone with help.
      “C:\xampp\php\php.exe” -f C:\xampp\htdocs\loadfiles\scripts\zip.php

      Set WinScriptHost = CreateObject(“WScript.Shell”)
      WinScriptHost.Run Chr(34) & “C:\AutoScriptRunner\script.bat” & Chr(34), 0
      Set WinScriptHost = Nothing

      Reply
        1. Chetan

          php script is not executed….
          but for every specified time the task is being executed….
          i want to send mail ( php script works perfectly if i run from my IDE).

          Reply

Leave a Reply

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