Previous Article

PHP Snippet to convert UK date to MySQL date

Next Article

This little bit of code is a great little time saver, while there are more specialised expressions that will check the different attributes of the date fall between specific numbers, this is simple and assumes the information has already been validated through checkdate or something client side.

$date = "23/06/1981";
$data = preg_replace("/(\d{2})[\/-](\d{2})[\/-](\d{4})/" , "\\3-\\2-\\1" , $data);
// $data would now be 1981-06-23

or as a function

function db_date($date) {
    $value = preg_replace("/(\d{2})[\/-](\d{2})[\/-](\d{4})/" , "\\3-\\2-\\1" , $data);
    return $value;
}

$data = db_date("23/06/1981");
// $data would be 1981-06-23

Example:

23/06/1981 returns 1981-06-23

Avatar of Clive Walkden

Clive Walkden

Posted:

Latest Articles

Linux command line tools, installations etc

Linux

How to Install NordLayer VPN Client on Ubuntu 20.04 and Connect to a Virtual Network

A simple to follow installation guide for NordLayer VPN

MySQL usage, tweaks and learnings

MySQL

Mastering MySQL Database Imports on Linux

Learn efficient ways to import MySQL databases on Linux using the mysql command-line client. Explore the --source option and < operator for seamless data migration. Master MySQL imports on Linux with our comprehensive guide.

DevOps principles and tool usage

DevOps

Mastering SSH Key Conversions for DevOps

A guide to convert SSH keys from one version to another using Linux CLI