note 48510 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 14 Aug 2005 11:51:27 +0000 |
| Subject: | note 48510 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-93597@lists.php.net to get a copy of this message | ||
Note Submitter: James McGuigan
----
A slightly more advanced method for converting European / UK dates (DD-MM-YYYY) to US dates
(MM-DD-YYYY) that
<?php
// helper function needed for date_uk_to_american($date)
// implode_range('|', 7, 11, true) returns "7|07|8|08|9|09|10|11"
function implode_range($seperator, $start, $end, $add_zeros=true) {
$array_int = range($start, $end);
if($add_zeros == false) { return implode($seperator,$array_int); }
else {
$array = array();
$digit_count = strlen("$end");
foreach($array_int as $number) {
$string = "$number";
$array[] = $string;
if(strlen($string) != $digit_count) {
$array[] = str_pad($string, $digit_count, '0', STR_PAD_LEFT);
}
}
return implode($seperator,$array);
}
}
// converts (DD-MM-YY) into (MM-DD-YY)
function date_uk_to_american($date) {
$days = implode_range('|',0,12,true);
$months = implode_range('|',0,30,true);
$year = '\d{2}|\d{4}';
//$whitespace = '[\W\/-\\\\]{1,3}';
$non_alpha = '[^0-9a-zA-Z]+';
return preg_replace( "/^($days)($non_alpha)($months)($non_alpha)($year)$/",
'$3$2$1$4$5', $date);
}
echo date_uk_to_american('3-12-82') . '<br>'; // 12-3-82
echo date_uk_to_american('9-31-2004') . '<br>'; // 9-31-2004 - non valid
uk date - not converted
echo date_uk_to_american('2004-12-01'). '<br>'; // 2004-12-01 - ISO date
-not converted
echo date_uk_to_american('01-02-04') . '<br>'; // 02-01-04
echo date_uk_to_american('04-02-01') . '<br>'; // 02-04-01 - can't
detect YY-MM-DD dates with 2 digit years between 2000 and 2012 - so it
?>