Helpers

Available Methods

formatDate()

Parses and formats date strings, with special handling for Microsoft JSON date format.

Signature

public static function formatDate(string $date, string $format = 'Y-m-d H:i:s'): string

Parameters

  • $date (string): The date string to format
  • $format (string, optional): The desired output format using PHP date format characters. Default: 'Y-m-d H:i:s'

Return Value

  • string: The formatted date string, or an empty string if the date cannot be parsed

Usage Examples

  1. Formatting a standard date string:
$formattedDate = Xero::formatDate('2023-09-15');
// Returns: '2023-09-15 00:00:00'

$formattedDate = Xero::formatDate('2023-09-15', 'd/m/Y');
// Returns: '15/09/2023'
  1. Handling Microsoft JSON date format:
$formattedDate = Xero::formatDate('/Date(1663257600000+0100)/');
// Returns the date formatted as 'Y-m-d H:i:s' with proper timezone handling

$formattedDate = Xero::formatDate('/Date(1663257600000+0100)/', 'Y-m-d');
// Returns just the date portion
  1. Error handling:
$formattedDate = Xero::formatDate('invalid-date');
// Returns: '' (empty string instead of throwing an exception)

formatQueryStrings()

Formats an array of parameters into a properly encoded query string for API requests.

Signature

public static function formatQueryStrings(array $params): string

Parameters

  • $params (array): Associative array of query parameters

Return Value

  • string: URL-encoded query string following RFC3986 standards

Usage Examples

  1. Basic parameter formatting:
$queryString = Xero::formatQueryStrings([
    'page' => 1,
    'limit' => 20
]);
// Returns: 'page=1&limit=20'
  1. Handling special characters:
$queryString = Xero::formatQueryStrings([
    'search' => 'Company & Partners',
    'status' => 'active'
]);
// Returns: 'search=Company%20%26%20Partners&status=active'
  1. Nested arrays:
$queryString = Xero::formatQueryStrings([
    'filter' => ['status' => 'ACTIVE', 'type' => 'ACCREC'],
    'order' => 'Date DESC'
]);
// Returns properly encoded query string with nested parameters

Notes

  • The formatDate() method is particularly useful when working with date responses from the Xero API, which may use Microsoft's JSON date format
  • The formatQueryStrings() method ensures proper URL encoding for all API requests, preventing issues with special characters

Copyright © 2006 - 2025 DC Blog - All rights reserved.