2011-08-31 104 views
2

用户输入日期为8/1/11或08/1/11。无论用户如何在我的文本字段中输入日期,我想将它们输入的内容转换为我的mm/dd/yyyy格式将用户输入转换为我的日期格式

因此,如果用户输入8/1/11,它会将其转换为08/01/2011。

+2

它很容易让用户自由输入**日期字符串**。 。你应该考虑应用一些用户界面来限制用户。 JavaScript UI示例 - http://docs.jquery.com/UI/Datepicker(只是一个示例) – ajreal

回答

2

使用strtotime()将其转换为时间值,然后mktime()将其转换为您想要的任何格式。

编辑:原来并不那么简单。

你不能只打印您所想,并将其转移到另一个地方的日期,这将是最好使用某种UI上的客户端,以确保输入匹配,一个很好的例子是jQueryUI Datepicker

1
<?php 

$userInput = '08/1/11'; // or = '8/1/11' or = '08/01/11' or = '01/8/11' 

$arr = explode('/', $userInput); 

$formatted = sprintf("%1$02d", $arr[0]) . '/' . sprintf("%1$02d", $arr[1]) . '/20' . $arr[2]; 

?> 
0
<?php 
preg_match('#^(0?[1-9]|1[0-2])/(0?[1-9]|[12][0-9]|3[01])/(\d{2})$#',trim($date),$matches); 
$month=str_pad($matches[1],2,'0',STR_PAD_LEFT); 
$day=str_pad($matches[2],2,'0',STR_PAD_LEFT); 
$year=$matches[3]; 

$result="{$month}/{$day}/{$year}"; 
?> 
1

选中此项。 它还检查日期是否有效(使用checkdate)并将年份由短到长转换。短期使用时,0-69转换为2000-2069,70-99转换为1970-1999。

<?php 
function cleanDate($input) 
{ 
    $parts = explode('/', $input); 
    if(count($parts) != 3) return false; 

    $month = (int)$parts[0]; 
    $day = (int)$parts[1]; 
    $year = (int)$parts[2]; 

    if($year < 100) 
    { 
     if($year < 70) 
     { 
      $year += 2000; 
     } 
     else 
     { 
      $year += 1900; 
     } 
    } 

    if(!checkdate($month, $day, $year)) return false; 

    return sprintf('%02d/%02d/%d', $month, $day, $year); 
    // OR 
    $time = mktime(0, 0, 0, $month, $day, $year); 
    return date('m/d/Y', $time); 
} 

$test = array(
    '08/01/2011', '8/1/11', '08/01/11', // Should all return 08/01/2011 
    '08/1/87', // Should return 08/01/1987 
    '32/1/93', '13', // Should fail: invalid dates 
    '02/29/2011', // Should fail: 2011 is not a leap year 
    '2/29/08'); // Should return 02/29/2008 (2008 is a leap year) 
foreach($test as $t) 
{ 
    echo $t.' : '.(cleanDate($t) ?: 'false')."\n"; 
} 
?> 

Result: 
08/01/2011 : 08/01/2011 
8/1/11 : 08/01/2011 
08/01/11 : 08/01/2011 
08/1/87 : 08/01/1987 
32/1/93 : false 
13 : false 
02/29/2011 : false 
2/29/08 : 02/29/2008 
0

虽然@Truth是正确的,用户可以做很多事情,让人很难,实际上有一种方法做分析的日期输入框工作的一个相当体面的工作。它考虑到可能出现的各种用户输入问题。

注意,它确实使两个假设:

本地使用M/d/Y
的日期格式。如果今年是短期形式进入,我们面对的是一个2000+一年

<?php 
    // Trim spaces from beginning/end 
    $date = trim(request($field)); 
    // Allow for the user to have separated by spaces 
    $date = str_replace(" ", "/", $date); 
    // Allow for the user to have separated by dashes or periods 
    $date = str_replace("-", "/", str_replace(".", "/", trim($date))); 
    // Explode the date parts out to ensure a year 
    // Granted, this is geo-centric - you could adjust based on your locale 
    $dateparts = explode("/", $date); 
    // Check for a year. If not entered, assume this year 
    if (!isset($dateparts[2])) {$dateparts[2] = date("Y");} 
    // Force year to integer for comparison 
    $dateparts[2] = (int)$dateparts[2]; 
    // Allow for user to use short year. Assumes all dates will be in the year 2000+ 
    if ($dateparts[2] < 2000) {$dateparts[2]+= 2000;} 
    // Re-assemble the date to a string 
    $date = implode("/", $dateparts);  
    // Utilize strtotime and date to convert date to standard format 
    $date = date("m/d/Y", strtotime($date)); 
?> 

声明:是的,这是做这件事的详细方式,但是我为了清晰度而不是效率。

相关问题