note 51652 added to function.date
| From: | bati1927 at msn dot com | Date: | Wed, 06 Apr 2005 16:48:58 +0000 |
| Subject: | note 51652 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87592@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51652
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51652&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51652&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51652&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51652&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51652&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51652&report=yes
Search -- http://master.php.net/manage/user-notes.php