Format Xero dates from the API

David Carr

Tutorials PHP & MySQL

Table of Contents

Dates in Xero that are not already set to a datestring are in a format like Date(1518685950940+0000)

How to format these

They are made up from unix timestamps in milliseconds rather than seconds, they need to be converted by taking the timestamp and times by 1,000

date("Y-m-d", 1518685950940 / 1000)

This will give you the formatted date.

When doing this over the API dynamically you can do this:

if (isset($inv['FullyPaidOnDate'])) {
    $date = str_replace('/Date(', '', $inv['FullyPaidOnDate']);
    $parts = explode('+', $date);
    $paidAt = date("Y-m-d", $parts[0] / 1000);
}

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.