PHP/MySQL dates

I have posted before as to a MySQL date, i.e. Y-m-d can be converted in a previous post here.

I found myself this evening in need of converting UK dates back into the MySQL format.

Now, the strtotime() function requires a MySQL-style date going in, so doesn’t work in the way I needed it to tonight.

Luckily I found the ukstrtotime() function online, which works great:

function ukstrtotime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}

This is then used in the following way:

$mysqldate = date('Y-m-d',ukstrtotime($ukdate));

Advertisements
Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: