note 51652 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Jan 2006 12:46:21 +0000 |
| Subject: | note 51652 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101840@lists.php.net to get a copy of this message | ||
Note Submitter: bati1927 at msn dot com
----
This function convert date format.
<?php
function dateFormat($input_date, $input_format, $output_format) {
preg_match("/^([\w]*)/i", $input_date, $regs);
$sep = substr($input_date, strlen($regs[0]), 1);
$label = explode($sep, $input_format);
$value = explode($sep, $input_date);
$array_date = array_combine($label, $value);
if (in_array('Y', $label)) {
$year = $array_date['Y'];
} elseif (in_array('y', $label)) {
$year = $year = $array_date['y'];
} else {
return false;
}
$output_date = date($output_format, mktime(0,0,0,$array_date['m'],
$array_date['d'], $year));
return $output_date;
}
$mydate = "16/04/02";
echo ('date input: '.$mydate.' -> date output: '.dateFormat($mydate,
'd/m/y', 'Y-m-d'));
?>
This code will output: 16/04/2002 -> 2002-04-16
if you don't use PHP5 you can implement array_combine function by:
<?php
if (!function_exists('array_combine')) {
function array_combine($a1, $a2) {
if(count($a1) != count($a2))
return false;
if(count($a1) <= 0)
return false;
$a1 = array_values($a1);
$a2 = array_values($a2);
$output = array();
for($i = 0; $i < count($a1); $i++) {
$output[$a1[$i]] = $a2[$i];
}
return $output;
}
}
?>