PHP - Turn UK Date into US Date

How to go about changing UK date to US date when using PHP

This is something I find myself needing to do quite frequently. You deal with UK dates as input and output of a system but somewhere in the middle they need to convert to US dates.

A quick way to shove a UK Date into a US format is to to an explode on the UK date by whichever separator it is using (normally '/', sometimes '-') then glue the bits back together with the second element swapped with the first (moving month and day around).

I have compiled this into a quick method:

/**
* A very simple function to change the date from the UK
* format to the American format.
*
* @param string $uk_date Assumed to be in the format day month year
* @param string $separator_input What divides the date up goung in.
* @param string $sepatator_output What divides the date up going out.
*
* @return string The date formatted to suit the US formatting.
*
* @author Toby Osbourn
*/

function makeUSDate($uk_date, $separator_input = '/', $sepatator_output = '/')
{
    list($day, $month, $year) = explode($separator_input, $uk_date);
    return $month.$sepatator_output.$day.$sepatator_output.$year;
}

Recent posts View all

Web DevProductivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.