Strings, Numbers & Dates
String, number, array and date helpers.
32 functions. All require apis/main_webservices.php to be included.
On this page: SecToTime ยท TimeToSec ยท a2c ยท a2q ยท addDaysToDate ยท at ยท base64_url_decode ยท base64_url_encode ยท bom ยท dateDiff ยท dtoc ยท dtoc2 ยท eom ยท escapeString ยท fig2wrd ยท generateRandomString ยท getSize ยท hasAlphabet ยท hasDigits ยท hex2str ยท lastcomma ยท ltoc ยท nocomma ยท padlex ยท padrex ยท proper ยท rat ยท removeNonSpecialChars ยท space ยท split ยท str2hex ยท xstr
SecToTime()
Prototype
SecToTime( $seconds )
Description
- Converts seconds into
hh:mm:sstime format. - Inverse of
TimeToSec().
Parameters
$secondsโ Seconds as Number
Returns
- String โ the time formatted as
hh:mm:ss.
Return example
echo SecToTime(3630);
// '01:00:30'
Usage example
$nsecs = SecToTime(5600);
:material-file-code: Source: apis/core2.php
TimeToSec()
Prototype
TimeToSec( $time )
Description
- Converts a clock time into the number of seconds elapsed since midnight.
- Use for time arithmetic and duration bucketing.
Parameters
$timeโ In HH:MM:SS format
Returns
- Integer โ seconds since 00:00:00.
Return example
echo TimeToSec('01:00:30');
// 3630
Usage example
$nsecs = TimeToSec( date('H:i:s') );
:material-file-code: Source: apis/core2.php
a2c()
Prototype
a2c( $xExpr, $case='U', $escstr=true )
Description
- Converts a value of any type to a SQL string literal, with optional case handling and escaping.
- Use to inline scalar values safely into queries.
Parameters
$xExprโ Variable or value to be converted to string- [$case]: Resulting Conversion Case. U for upper, L for Lower, N for natural. Defaults to U
- [$escstr]: Escape Result String as per Sql formatting. Defaults to true.
Returns
- String โ the value rendered as a SQL-safe string.
Return example
$v = a2c("O'Brien");
// "'O''Brien'" (escaped & quoted)
Usage example
$nID = 100;
$q = 'SELECT id, name FROM #fasmast WHERE id=' . a2c($nID);
returns: 'SELECT id, name FROM pj_myproject_fasmast WHERE id=100';
:material-file-code: Source: apis/core2_a.php
a2q()
Prototype
a2q( $list, $forsql=true, $escstr='' )
Description
- Converts a PHP array into a SQL-ready, comma-separated list of values.
- Use to build safe
IN (...)clauses; values are quoted/escaped per the flags.
Parameters
$listโ array containing any values- [$forsql]: Default true: Each value will be converted and delimited by proper string end markers.
- [$escstr]: Default '': Escape string for each value
Returns
- String โ comma-separated (optionally quoted/escaped) value list.
Return example
$in = a2q(['A','B','C']);
$sql = "... WHERE code IN ($in)";
// $in = "'A','B','C'"
Usage example
$aPartyIDs = array( 3,5,56,62 );
$cQ = 'SELECT invoice, date, netamt FROM #trn_sales WHERE party IN (' . a2q( $aPartyIDs ). ')';
returns: 'SELECT invoice, date, netamt FROM pj_yourproject_trn_sales WHERE party IN (3,4,56,62)';
:material-file-code: Source: apis/core2_a.php
addDaysToDate()
Prototype
addDaysToDate($date,$days)
Description
- Adds a number of days to a date and returns the resulting date.
- Accepts negative days to subtract.
Parameters
- <$date>: Date to which days are to be added
- <$days>: No. of days to be added
Returns
- Date string โ the date shifted by the given number of days.
Return example
echo addDaysToDate('2025-03-31', 5);
// '2025-04-05'
Usage example
$newdate = addDaysToDate('2024-07-01', 10)
returns: 2024-07-11
:material-file-code: Source: apis/core2_a.php
at()
Prototype
at( $cExp, $cString )
Description
- Returns the position of a substring within a string (0-based), case-insensitively.
- Returns -1 when the substring is not found.
Parameters
$cExprโ Substring to be checked for presence in $cString$cStringโ String in which the substring is to be checked for
Returns
- Integer โ 0-based position of the match, or -1 if absent.
Return example
echo at('lo', 'Hello');
// 3 (returns -1 if not found)
Usage example
$pos = at( 'gr', 'GREAT' ) -> 0
$pos = at( 'rat', 'pirate' ) -> 2
:material-file-code: Source: apis/core2_a.php
base64_url_decode()
Prototype
base64_url_decode($val)
Description
- Decodes a URL-safe base64 value produced by
base64_url_encode(). - Inverse operation for reading encoded query payloads.
Parameters
- <$val>: Value to be encoded as base64
Returns
- String โ the decoded original text.
Return example
echo base64_url_decode('aWQ9NDImeD0x');
// 'id=42&x=1'
Usage example
$str = base64_url_decode($base64_Encoded_String);
:material-file-code: Source: apis/core2_a.php
base64_url_encode()
Prototype
base64_url_encode($val)
Description
- Returns a URL-safe base64 encoding of a value.
- Use to pass binary/encoded payloads safely in query strings.
Parameters
- <$val>: Value to be encoded as base64
Returns
- String โ URL-safe base64 text.
Return example
$e = base64_url_encode('id=42&x=1');
// e.g. 'aWQ9NDImeD0x'
Usage example
$str = base64_url_encode('https://merciglobal.com');
:material-file-code: Source: apis/core2_a.php
bom()
Prototype
bom($month=0)
Description
- Returns the first day of the month (
YYYY-MM-01), optionally offset by a month. - Use to anchor month-start period boundaries.
Parameters
$monthโ Defaults to current month
Returns
- Date string โ the beginning of the month.
Return example
echo bom();
// e.g. '2025-06-01'
Usage example
$bom = bom(3);
returns: '2024-03-01'
:material-file-code: Source: apis/core2_a.php
dateDiff()
Prototype
dateDiff( $d1, $d2 )
Description
- Returns the difference in days between two dates.
- Use for ageing, due-day and duration calculations.
Parameters
$d1โ First Date$d2โ Second Date
Returns
- Integer โ number of days between the two dates.
Return example
echo dateDiff('2025-03-01', '2025-03-31');
// 30
Usage example
$days = dateDiff( '2017-03-31', '2017-03-01' );
returns: 31
$days = dateDiff( '2017-03-01', '2017-03-31' );
returns: 31
Both above examples return same values.
:material-file-code: Source: apis/core2_a.php
dtoc()
Prototype
dtoc( $xdate, $separator='-' )
Description
- Converts a date value to
dd-mm-yyyydisplay format with a configurable separator. - Use to present database dates in Indian display style.
Parameters
$xdateโ Any standard date value as per SQL standards of yyyy-mm-dd$separatorโ Default to hyphen (-).
Returns
- String โ the formatted
dd-mm-yyyydate.
Return example
echo dtoc('2025-03-31');
// '31-03-2025'
Usage example
$cDate = dtoc( '2017-03-31' ) --> '31-03-2017'
:material-file-code: Source: apis/core2_a.php
dtoc2()
Prototype
dtoc2( $xdate, $separator='-' )
Description
- Alternate date-to-
dd-mm-yyyyformatter with a configurable separator. - Use where a slightly different formatting variant is required.
Parameters
$xdateโ Any standard date value as per SQL standards of yyyy-mm-dd$separatorโ Default to hyphen (-).
Returns
- String โ the formatted date string.
Return example
echo dtoc2('2025-03-31', '/');
// '31/03/2025'
Usage example
$cDate = dtoc2( '2017-03-31' ) --> '31-03-2017'
:material-file-code: Source: apis/core2_a.php
eom()
Prototype
eom($month=0)
Description
- Returns the last day of the month (
Y-m-t), optionally offset by a month. - Use to anchor month-end period boundaries.
Parameters
$monthโ Defaults to current month
Returns
- Date string โ the end of the month.
Return example
echo eom();
// e.g. '2025-06-30'
Usage example
$eom = eom(3);
returns: '2024-03-31'
:material-file-code: Source: apis/core2_a.php
escapeString()
Prototype
escapeString($str)
Description
- Escapes a string into MySQL-safe form.
- Use when you must inline a value into SQL outside the normal helpers.
Parameters
- <$str>: String to Escape
Returns
- String โ the MySQL-escaped string.
Return example
$s = escapeString("O'Brien");
// "O\'Brien"
Usage example
$qry = escapeString($qry);
:material-file-code: Source: apis/core4.php
fig2wrd()
Prototype
fig2wrd( $number, $cCurr='Rupees', $cPaise='Paise', $cOnly='Only' )
Description
- Converts a number/amount into words, with configurable currency and paise wording.
- Use to print amounts-in-words on invoices and cheques.
Parameters
$numberโ Amount / number to convert to string- [$cCurr]: Default: Rupees, Currency Prefix
- [$cPaise]: Default: Paise, Currency Units
- [$cOnly]: Default: Only, Suffix of string
Returns
- String โ the amount spelled out in words.
Return example
echo fig2wrd(1250.50);
// 'Rupees One Thousand Two Hundred Fifty and Paise Fifty Only'
Usage example
$converted = fig2wrd( 1000 ) --> One Thousand Ruppes Only
:material-file-code: Source: apis/core2.php
generateRandomString()
Prototype
generateRandomString($length = 10,$alpha=true)
Description
- Generates a random string of a given length, alphanumeric or alphabetic.
- Use for tokens, temporary passwords and unique suffixes.
Parameters
- <$length>: Length of string
- <$alpha>: TRUE if alphabets are included with numbers. FALSE if only numbers are included
Returns
- String โ a random string of the requested length.
Return example
echo generateRandomString(8);
// e.g. 'Xk29Pq7m'
Usage example
$rand = generateRandomString(10,true);
:material-file-code: Source: apis/core2_a.php
getSize()
Prototype
getSize( $array )
Description
- Returns the size/length of an array (a safe
count()wrapper). - Returns 0 for empty/uninitialised values, avoiding warnings.
Returns
- Integer โ the number of elements in the array.
Return example
echo getSize(['a','b','c']);
// 3
Usage example
$names = array('test','ram','shyam');
$tlen = getSize( $names ); will return 3
:material-file-code: Source: apis/core2_a.php
hasAlphabet()
Prototype
hasAlphabet( $cString )
Description
- Checks whether a string contains any alphabetic character.
- Use to validate that an input isn't purely numeric/symbolic.
Parameters
- <$cString> : string to be checked
Returns
- Boolean โ
trueif at least one letter is present.
Return example
var_dump(hasAlphabet('12A4'));
// true
:material-file-code: Source: apis/core.php
hasDigits()
Prototype
hasDigits($value)
Description
- Checks the digit composition of a value.
- Returns FALSE when alphabetic characters are present; TRUE when only digits are present.
Parameters
- [$value]: variable to check for presence of only digits
Returns
- Boolean โ
trueif the value is digits-only,falseif it contains letters.
Return example
var_dump(hasDigits('12345'));
// true
var_dump(hasDigits('12A45'));
// false
Usage example
$isnumber = hasDigits('1234a');
:material-file-code: Source: apis/core4.php
hex2str()
Prototype
Hex2Str( $cHexStr )
Description
- Decodes a HEX string back into its normal text form.
- Inverse of
str2hex(); use for values stored/transmitted as hex.
Parameters
- <$cHexStr>: Hex encoded string
Returns
- String โ the decoded plain text.
Return example
echo Hex2Str('48656c6c6f');
// 'Hello'
:material-file-code: Source: apis/core2_a.php
lastcomma()
Prototype
lastcomma( $cStr )
Description
- Appends a trailing comma to a non-empty string.
- Companion to
nocomma()when accumulating list fragments.
Parameters
$cStrโ String to which comma is to be added.
Returns
- String โ the input with a trailing comma added (empty stays empty).
Return example
echo lastcomma('a,b');
// 'a,b,'
Usage example
$str = lastcomma( 'hello everyone' );
returns: 'hello everyone,'
:material-file-code: Source: apis/core2_a.php
ltoc()
Prototype
ltoc( $logicalvalue )
Description
- Converts a logical/boolean value to the string
'true'or'false'. - Use when emitting JS/JSON-friendly text from PHP booleans.
Parameters
$logicalvalueโ Logical Value to convert to string
Returns
- String โ
'true'or'false'.
Return example
echo ltoc(5 > 3);
// 'true'
Usage example
$lcheck = True;
$cTrueOrFalse = ltoc( $lcheck );
:material-file-code: Source: apis/core2_a.php
nocomma()
Prototype
nocomma( $cStr )
Description
- Removes a trailing comma from the end of a string, if present.
- Use after building a comma-joined list to drop the dangling separator.
Parameters
$cStrโ String from which comma is to be removed.
Returns
- String โ the input without a trailing comma.
Return example
echo nocomma('a,b,c,');
// 'a,b,c'
Usage example
$str = lastcomma( 'hello everyone,' );
returns: 'hello everyone'
:material-file-code: Source: apis/core2_a.php
padlex()
Prototype
padlex( $cStr, $nLen, $cPad=' ' )
Description
- Left-pads a string to a given length using a pad character (default
). - Common for zero-padding numbers and right-aligning text.
Parameters
$cStrโ String to pad$nlenโ Final length of string$cPadโ Padding character
Returns
- String โ the left-padded string of the requested length.
Return example
echo padlex('12', 5, '0');
// '00012'
Usage example
$cNewStr = padlex( 'Test String', 14, '*' )
returns: '***Test String'
:material-file-code: Source: apis/core2_a.php
padrex()
Prototype
padrex( $cStr, $nLen, $cPad=' ' )
Description
- Right-pads a string to a given length using a pad character (default
). - Use for fixed-width alignment in HTML/printed output.
Parameters
$cStrโ String to pad$nlenโ Final length of string$cPadโ Padding character
Returns
- String โ the right-padded string of the requested length.
Return example
echo padrex('12', 5, '0');
// '12000'
Usage example
$cNewStr = padrex( 'Test String', 14, '*' )
returns: 'Test String***'
:material-file-code: Source: apis/core2_a.php
proper()
Prototype
proper( $cString )
Description
- Converts a string to proper case โ the first letter of each word capitalised.
- Use to normalise names and titles for display.
Parameters
$cStringโ Word or Phrase to be converted.
Returns
- String โ the proper-cased text.
Return example
echo proper('rajesh kumar');
// 'Rajesh Kumar'
Usage example
$name = proper( 'INDIA IS great' );
returns: India Is Great
:material-file-code: Source: apis/core2_a.php
rat()
Prototype
rat( $cExp, $cString )
Description
- Returns the last position of a substring within a string (0-based), case-insensitively.
- Returns -1 when not found โ the reverse-search counterpart of
at().
Parameters
$cExprโ Substring to be checked for presence in $cString$cStringโ String in which the substring is to be checked for
Returns
- Integer โ 0-based position of the last match, or -1 if absent.
Return example
echo rat('l', 'Hello');
// 3
Usage example
$pos = rat( 'gr', 'GREAT' ) -> 0
$pos = rat( 'rat', 'pirate' ) -> 2
:material-file-code: Source: apis/core2_a.php
removeNonSpecialChars()
Prototype
removeNonSpecialChars($str,$include='')
Description
- Strips non-visible / non-standard characters from a string, optionally keeping an allowed set.
- Use to sanitise pasted text before storing or matching.
Returns
- String โ the cleaned text with disallowed characters removed.
Return example
echo removeNonSpecialChars("He\tllo\x00");
// 'Hello'
Usage example
$str = 'This is a Special~ String#'
$tlen = removeNonSpecialChars( $str, $include='#'); Will return: This is a Special String#
:material-file-code: Source: apis/core2_a.php
space()
Prototype
space($nctr=0)
Description
- Returns a run of non-breaking spaces (
) repeated as requested. - Use for simple HTML spacing/padding in generated markup.
Parameters
$nctrโ No. of spaces
Returns
- String โ the requested number of
entities.
Return example
echo 'A'.space(3).'B';
// 'A B'
Usage example
$name = 'INDIA' . space(1) . 'IS' . space(1) . 'Great';
returns: INDIA IS Great
:material-file-code: Source: apis/core2_a.php
split()
Prototype
rat( $cExp, $cString )
Description
- String search/split helper (documented alongside
at/rat) returning a position into the string. - Behaves like the position helpers โ 0-based, case-insensitive, -1 when not found.
Parameters
$cExprโ Substring to be checked for presence in $cString$cStringโ String in which the substring is to be checked for
Returns
- Integer โ position result, or -1 if not found.
Return example
$pos = split(',', 'a,b,c');
// position of separator
Usage example
$pos = rat( 'gr', 'GREAT' ) -> 0
$pos = rat( 'rat', 'pirate' ) -> 2
:material-file-code: Source: apis/core2_a.php
str2hex()
Prototype
Str2Hex( $cStr )
Description
- Encodes a string to HEX.
- Use for safe transport/storage of arbitrary bytes as hex.
Parameters
- <$cStr>: Normal String to be encoded into Hex
Returns
- String โ the hex-encoded representation.
Return example
echo Str2Hex('Hello');
// '48656c6c6f'
:material-file-code: Source: apis/core2_a.php
xstr()
Prototype
xstr($numb,$dec=2,$money=false)
Description
- Formats a number with thousands separators and fixed decimals, optionally as money.
- Pair with Indian Cr/Lakh conventions for financial display.
Parameters
$numbโ Any number$decโ Dicimal places
Returns
- String โ the comma-formatted number with the requested decimals.
Return example
echo xstr(1234567.5, 2);
// '12,34,567.50' (Indian grouping)
Usage example
$cStr = xstr( 12345, 3) --> '12345.000'
:material-file-code: Source: apis/core2_a.php