ShiftLib Functions

This is a list of useful functions that are provided by ShiftLib


int age( string $date )

Calculates and returns the age from a date.


alert( string $error )

Outputs a javascript alert with displaying the supplied error message.


current_tab( string $tab )

If the tab matches the first level of the URI then it will output ' class="current over"'.
You can use this with CSS to highlight the current section of the navigation.


date_diff( string $endDate, string $beginDate )

returns the difference in days between the provided dates.


dateformat( string $format, string $date )

Similar to PHP date function - but accepts date in the format of YYYY-MM-DD'. Which is the native mysql format.


string escape( string $string )

An alias for the PHP function mysql_real_escape_string


string file_ext( string $file )

Returns the file extension part of a file name.


string file_size( int $size )

Converts a file size in bytes to a string e.g. 7000 = 7K


string format_mobile( string $mobile )

Remove invalid characters and prefixes UK internation dialing code.


string format_postcode(string $postcode)

Removes invalid chracters and adds correct spacing. Returns false if the postcode is completely invalid.


string generate_password()

Returns a random 6 character password.


string html_options( array $options,mixed $selected, bool $force_assoc=false )

Takes any type of array and returns a list of html options for use in a select element e.g. <option value="email">Email</option>

The selected argument will return the matching options as selectedd e.g. <option value="email" selected>Email</option>

The function will try to distinguish if options are an associative array. In some circumstances it is impossible to tell and it will defaul to non-associative.

You can over-ride this by setting $force_assoc to true.


mixed image($file, $w, $h, $attribs=true, $crop=false)

Returns a thumbnail link

Example #1 Thumbnail image tag

This will show a 320x240 thumbnail for the specified image.

<?=image($v['image'], 320, 240 'class="image"');?>


bool is_alphanumeric( string $string )

Checks if a string is alphanumeric


bool is_assoc_array( array $array)

Checks if an array is assoicative.


bool is_domain( string $domain )

Checks if a string is a valid domain


bool is_email( string $email )

Checks if a string is a valid email address


bool is_nino( string $nino )

Checks if a string is a valid UK national insurance number.


bool is_postcode( string $postcode )

Checks if a string is a valid UK postcode


bool is_url( string $url )

Checks if a string is a valid url.


load_js( mixed $libs )

Used for loading AJAX libraries e.g.

<? load_js('lightbox'); ?>

Or for more than one:

<? load_js(array('lightbox','swfobject')); ?>

This should be placed in the head section of template.php.
Where supported the libraries will be loaded via a CDN (content delivery network), resulting in faster loading.
The following AJAX options are supported:

  • jquery
  • jqueryui
  • lightbox
  • cycle
  • swfobject
  • bootstrap
  • cms

Dependencies are automatically worked out. So for example if you include lightbox - it will automatically load jquery. Only call load_js once to avoid loading jquery multiple times.
It will load the latest versions of the libraries. It will detect if SSL is enabled and load SSL versions if neccessary.

CMS is a shiftlib script used for form validation.


redirect( $url, bool $_301=false )

Issues a header redirect to the provided URL. Adds a 301 header if $_301 is set to true.


string sec2hms ( int $sec, bool $padHours = false )

Converts seconds to HH:MM:SS


string spaced( string $str )

Replaces underscores with spaces.


array sql_query( string $query )

Execures a sql query with error handling. Returns an associative array of rows.


bool table_exists( string $table )

Checks if a table exits


string truncate( string $string, int $max = 50, string $rep = '..' )

Shortens a string to a length of $max characters. Appends $rep to the string.


string underscored( string $st r)

Replaces spaces with underscores.


array validate( array $fields,array $required )

Compares the array of $fields against the array of $required fields.
Any missing or invalid fields are returned.