【发布时间】:2012-08-13 14:55:24
【问题描述】:
我有一个用户格式化的日期和使用 PHP 日期格式 (http://fr.php.net/manual/en/function.date.php) 的对应格式。这种格式在用户输入之前是未知的。
我必须将此日期转换为已知格式,例如“d/m/Y h:i:s”。 我的 PHP 服务器使用的是 5.2.1 PHP 版本,所以我不能使用 DateTime 类。
有人有 DateTime::createFromFormat 方法的替代方法,这样的脚本或可以帮助我的想法?
最好的问候, 弗洛里安。
编辑:
我编写了一个函数,可以在不知道原始日期格式的情况下将日期转换为其他格式:
/**
* This function converts a date with unknown format into a defined format.
* It could detect some standard date formats : dd/mm/YYYY, dd-mm-YY, YYYY-mm-dd hh:mm:ss, etc.
* @param string $date date which is user formatted.
* @param int $options used to help algorithm to detect date format : US or FR date, past date or future date, hour format.
* @param string $newFormat format the date must be converted into
* @param string $originalFormat format detected
* @return boolean|string the new date, corresponding to the $date argument, formatted into the $newFormat format
*/
function convertDateToFormat($date, $options = 0, $newFormat, &$originalFormat = ''){
if($options == 0){
$options = DATE_FR | DATE_PAST | HOUR_24;
}
$matches = array();
$year = 0;
$month = 0;
$day = 0;
$hour = 0;
$minute = 0;
$second = 0;
$originalFormatOk = false;
if(preg_match('#(\d{1,2})([/.-])(\d{1,2})(([/.-])(\d{2,4}))?#', $date, $matches)){
if(isset($matches[6])){
if(strlen($matches[6]) == 2){
if($matches[6] >= date('y') && DATE_PAST)
$year = '19'.$matches[6];
else
$year = '20'.$matches[6];
$originalFormatyear = 'y';
if($options & DATE_US){
$originalFormat = 'm'.$matches[2].'d'.$matches[5].$originalFormatyear;
$year = $matches[6];
$month = $matches[1];
$day = $matches[3];
$originalFormatOk = true;
}
}
else{
$year = $matches[6];
$originalFormatyear = 'Y';
}
if(!$originalFormatOk){
$year = $matches[6];
$month = $matches[3];
$day = $matches[1];
$originalFormat = 'd'.$matches[2].'m'.$matches[5].$originalFormatyear;
$originalFormatOk = true;
}
}
else{
if($options & DATE_US){
$originalFormat = 'm'.$matches[2].'d';
$year = date('Y');
$month = $matches[1];
$day = $matches[3];
}
else{
$originalFormat = 'd'.$matches[2].'m';
$year = date('Y');
$month = $matches[3];
$day = $matches[1];
}
$originalFormatOk = true;
}
}
if(preg_match('#'.$matches[0].'(\D*)(\d{1,2})([:hH])(\d{1,2})(:(\d{1,2}))?#', $dateHour, $matches)){
if($options & HOUR_12)
$originalFormatHour = 'h';
else
$originalFormatHour = 'H';
$hour = $matches[2];
$minute = $matches[4];
if(strtolower($matches[3]) == 'h')
$matches[3] = '\\'.$matches[3];
$originalFormat .= $matches[1].$originalFormatHour.$matches[3].'i';
if(isset($matches[6])){
$second = $matches[6];
$originalFormat .= ':s';
}
}
if($originalFormatOk)
return date($newFormat, mktime($hour, $minute, $second, $month, $day, $year));
return false;
}
【问题讨论】:
-
不完全确定你的意思,但在 php 中你可以使用 strtotime 函数来尝试破译输入,即 date('d/m/Y h:i:s', strtotime('yesterday '));
-
你能给我们一个你想要转换的日期的例子吗?
-
为什么不能简单地使用
$tstamp = strtotime($userdate)然后date('d/m/Y h:i:s', $tstamp)? -
对于@therao:日期以已知格式提交,需要解码成通用格式。因此,如果用户提交 10/11/2012 并且他们是 UK 转换为 MySQL 2012/11/10,因为格式为 dd/mm/YYYY。但如果美国转换为 2012/10/11 格式为 mm/dd/YYYY?
-
如果日期以美国以外的格式提交,则 strtotime() 将无法正确工作,例如美国日期是 mm/dd/yyyy,而英国是 dd/mm/yyyy。如果使用 MySQL 格式 yyyy-mm-dd hh:ii:ss 那么作为 strtotime() 处理会更容易,比较和数据库存储会更容易。
标签: php date datetime format date-format