使用简单的用户输入格式转换自定义日期格式

时间:2013-07-17 19:00:28

标签: php

我需要从各种来源导入包含各种格式日期的文本文件。鉴于我到目前为止收集的测试文件中遇到了多少混乱的数据,我决定强制用户在导入文件之前输入日期格式,例如: MMDDYYYYDD.MM.YYYY等。

我不能指望这些用户能够理解足以输入Y-m-dn/j/Y格式,这些格式很容易解析,而且我不能依赖“标准”,例如“if a dash”使用,然后一个月来到日之前“等等,所以自动解析是非常不可能的。

通常的PHP日期格式化的另一个问题是我无法预测任何日期格式是否会有单个或两个数字,如果我给他们一个,我不知道createFromFormat如何解析它单个数字,当它需要一个两位数......

所以我的第一个想法是首先将纯数字日期与其他日期分开,并强制用户输入一个完整的8位数字日期以及格式代码 - 否则它会变得混乱。但到目前为止,这已经到了......

我考虑过并排分析日期和格式字符串,但是如果我不小心的话,这又会导致一位数的问题。

我没有找到任何对我的情况有帮助的东西,但也许我从错误的角度来看这个 - 也许有一个Javascript或jQuery日期格式选择工具,它与PHP日期格式兼容..

有关最佳方法的任何建议吗?

3 个答案:

答案 0 :(得分:0)

jquery ui有一个很好的日期选择器,我在几个应用程序中使用它以确保日期的格式始终相同

答案 1 :(得分:0)

我想我找到了最简单的解决方案。我觉得太复杂了。

我会将日期格式输入限制为M的2位数,D的2位数和Y的4位数,任何或不使用分隔符,然后我可以使用:

$str = '02-01-2013';

$format = "DD-MM-YYYY";
$matrix = array(
    "YYYY"=>"Y",
    "MM"=>"m",
    "DD"=>"d"
);
foreach ($matrix as $key=>$value)
{
    $format = str_replace($key, $value, $format);
}

$date = Datetime::createFromFormat($format, $str);
echo $date->format('Y-m-d');

这对我有用。谢谢你的建议!如果有人有更好的想法,我仍然是开放的: - )

答案 2 :(得分:0)

让用户从下拉菜单中选择预定义格式,而不是让用户输入日期格式。这样你就可以避免一个完全疯狂的人输入“月日 - YYYY”作为格式或其他奇怪的东西,甚至只是拼写错误。一旦你有一个格式列表,你可以使用php if语句将日期转换为你想要的任何完成格式。

$dateFormat = $_POST['dateFormat'];    
$date = $dateFromFile;
if ($dateFormat == "YYYYMMDD"){
   $cleanDate = DateTime::createFromFormat('Ymd', $date);
   echo $cleanDate->format('Y-m-d');
}
else if ($dateFormat == "YYYY/MM/DD"){
   $explodedDate = explode("/", $date);
   $year = $explodedDate[0];
   $month = $explodedDate[1];
   $day = $explodedDate[2];
   $cleanDate = "$year-$month-$day";
   echo $cleanDate;
} 
else if ($dateFormat == "Month DD, YYYY"){
    $explodedDate = explode(" ", $date);
    $month = $explodedDate[0]; 
            //change month from word into corresponding number
        if ($month == "January" || $month == "january" || $month == "Jan" || $month == "jan" || $month == "Jan." || $month == "jan."){
        $cleanMonth = "01";
        }
        else if ($month == "February" || $month == "february" || $month == "Feb" || $month == "feb" || $month == "Feb." || $month == "feb."){
        $cleanMonth = "02";
        }
        else if ($month == "March" || $month == "march" || $month == "Mar" || $month == "mar" || $month == "Mar." || $month == "mar."){
        $cleanMonth = "03";
        }
        else if ($month == "April" || $month == "april" || $month == "Apr" || $month == "apr" || $month == "Apr." || $month == "apr."){
        $cleanMonth = "04";
        }
        else if ($month == "May" || $month == "may" || $month == "May" || $month == "may" || $month == "May." || $month == "may."){
        $cleanMonth = "05";
        }
        else if ($month == "June" || $month == "june" || $month == "Jun" || $month == "jun" || $month == "Jun." || $month == "jun."){
        $cleanMonth = "06";
        }
        else if ($month == "July" || $month == "july" || $month == "Jul" || $month == "jul" || $month == "Jul." || $month == "jul."){
        $cleanMonth = "07";
        }
        else if ($month == "August" || $month == "august" || $month == "Aug" || $month == "aug" || $month == "Aug." || $month == "aug."){
        $cleanMonth = "08";
        }
        else if ($month == "September" || $month == "september" || $month == "Sep" || $month == "sep" || $month == "Sep." || $month == "sep."){
        $cleanMonth = "09";
        }
        else if ($month == "October" || $month == "october" || $month == "Oct" || $month == "oct" || $month == "Oct." || $month == "oct."){
        $cleanMonth = "10";
        }
        else if ($month == "November" || $month == "november" || $month == "Nov" || $month == "nov" || $month == "Nov." || $month == "nov."){
        $cleanMonth = "11";
        }
        else if ($month == "December" || $month == "december" || $month == "Dec" || $month == "dec" || $month == "Dec." || $month == "dec."){
        $cleanMonth = "12";
        }   
    $day = $explodedDate[1];
    $year = $explodedDate[2];
    $cleanDay = preg_replace("/[^0-9]/","",$day);//remove extra characters from day
    $cleanDate = "$year-$cleanMonth-$cleanDay";
    echo $cleanDate;
}

您必须找到一种方法来转换您能想到的每种不同日期格式。所以需要一些游戏。您还可以向用户提供选项,以通知网站管理员未列出的日期格式。然后获取该选项以向您发送电子邮件,以便将其添加到列表中。