<?php
function convertdate($s_date,$s_from,$s_to,$s_return_delimiter) {
$s_return_date = '';
$s_from = strtolower($s_from);
$s_to = strtolower($s_to);
$s_date = str_replace(array('\'', '-', '.', ',', ' '), '/', $s_date);
$a_date = explode('/', $s_date);
switch($s_from) {
case 'eng': $day = $a_date[0];
$month = $a_date[1];
$year = $a_date[2];
break;
case 'usa': $month = $a_date[0];
$day = $a_date[1];
$year = $a_date[2];
break;
case 'iso': $year = $a_date[0];
$month = $a_date[1];
$day = $a_date[2];
break;
default: user_error('function convertdate(string $s_date, string $s_from, string $s_to, string $s_return_delimiter) $s_from not a valid type of \'eng\', \'usa\' or \'iso\'');
return NULL;
}
if (strlen($day)==1) { $day='0'.$day; } if (strlen($month)==1) { $month='0'.$month; } if (strlen($year)==3) { $year=substr(date('Y'),0,strlen(date('Y'))-3).$year; } if (strlen($year)==2) { $year=substr(date('Y'),0,strlen(date('Y'))-2).$year; } if (strlen($year)==1) { $year=substr(date('Y'),0,strlen(date('Y'))-1).$year; } switch($s_to) {
case 'eng': $s_return_date = $day.$s_return_delimiter.$month.$s_return_delimiter.$year;
break;
case 'usa': $s_return_date = $month.$s_return_delimiter.$day.$s_return_delimiter.$year;
break;
case "iso": $s_return_date = $year.$s_return_delimiter.$month.$s_return_delimiter.$day;
break;
default: user_error('function convertdate(string $s_date, string $s_from, string $s_to, string $s_return_delimiter) $s_to not a valid type of \'eng\', \'usa\' or \'iso\'');
return NULL;
}
if (!is_numeric($month) || !is_numeric($day) || !is_numeric($year)) {
return NULL;
} elseif (!checkdate($month, $day, $year)) {
return NULL;
}
return $s_return_date;
}
echo convertdate('13/04/2009','eng','iso','-');
?>