How to Change Date from dd/mm/yyyy to yyyy-dd-mm in PHP

Change Date from dd/mm/yyyy to yyyy-dd-mm for MYSQL Friendly is simple.

$date_array = explode("/",$date); // split the array
$var_day = $date_array[0]; //day seqment
$var_month = $date_array[1]; //month segment
$var_year = $date_array[2]; //year segment
$new_date_format = "$var_year-$var_day-$var_month"; // join them together

Possibly a more MySQL friendly format in some circumstances and Convert date from YYYY/MM/DD to DD.MM.YYYY (and from DD.MM.YYYY to YYYY/MM/DD)

/**
* @param string $date (d.m.y, y-m-d, y/m/d)
* @return string|bol
*/

function convertDate($date) {
// EN-Date to GE-Date
if (strstr($date, "-") || strstr($date, "/")) {
$date = preg_split("/[\/]|[-]+/", $date);
$date = $date[2].".".$date[1].".".$date[0];
return $date;
}
// GE-Date to EN-Date
else if (strstr($date, ".")) {
$date = preg_split("[.]", $date);
$date = $date[2]."-".$date[1]."-".$date[0];
return $date;
}
return false;
}

Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation