Time Module

The Time module provides time based code execution for XPSPL.

Installation

The Time Module is bundled with XPSPL as of version 2.0.0.

XPSPL

XPSPL >= 2.0

Configuration

The Time Module has no runtime configuration options available.

Usage

Importing

<?php

xp_import('time');

Note

By default all times are in seconds.

Timeouts

Timeouts are functions executed once after a specific amount of time.

Timeouts are supported using the awake and xp_exhaust functions.

The below example demonstrates executing a timeout after 30 seconds.

<?php
// Import the time module.
xp_import('time');

// Define the exhaust rate of 1.
time\awake(30, xp_exhaust(1, function(){
    echo '30 seconds just passed';
});

Intervals

Intervals are functions executed continously after a specific amount of time.

The example below demonstrates an interval that runs every 30 seconds.

<?php

xp_import('time');

time\awake(30, function(){
    echo '30 seconds passed';
});

Milliseconds and Microseconds Instructions

The wait time period can be defined as seconds, milliseconds and microseconds.

Defining the instruction of time can be done by providing the $instruction to the awake function usign the time instruction constants.

  • TIME_SECONDS Second instruction
  • TIME_MILLISECONDS Millisecond instruction
  • TIME_MICROSECONDS Microsecond instruction

The example below demonstrates executing timeouts in seconds, milliseconds and microseconds.

Warning

Microsecond precision CANNOT be guaranteed.

<?php

xp_import('time');

// SECONDS
time\awake(30, function(){
    echo '30 seconds just passed';
}, TIME_SECONDS);

// MILLISECONDS
time\awake(1000, function(){
    echo '1000 milliseconds just passed';
}, TIME_MILLISECONDS);

// SECONDS
time\awake(1000, function(){
    echo '1000 microseconds just passed';
}, TIME_MICROSECONDS);

CRON Time

The CRON syntax is supported for executing signals based on the CRON syntax using the CRON api function.

The example below demonstrates executing a signal everyday at 12pm.

<?php
// CRON
time\CRON('12 * * * *', function(){
    echo 'It is 12 oclock!';
});

API

All functions and classes are under the time namespace.

time\awake($time, $callback, [$instruction = 4])

Wakes the system loop and runs the provided function.

Parameters:
  • integer – Time to wake in.
  • callable – Callable function.
  • integer – The time instruction. Default = Seconds
Return type:

array [signal, process]

time\CRON($cron, $process)

Wakes the system using the Unix CRON expressions.

If no priority is provided with the `$process` it is set to null.

Parameters:
  • string – CRON Expression
  • callable – Callback function to run
Return type:

array [signal, process]

Read the Docs v: v5.0.1
Versions
latest
v5.0.1
v5.0.0
v4.0.1
v4.0.0
v3.1.1
v3.1.0
v3.0.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.