Php 将日期从一种格式转换为另一种格式
可能重复:Php 将日期从一种格式转换为另一种格式,php,date,datetime,format,date-format,Php,Date,Datetime,Format,Date Format,可能重复: 我有一个用户格式化的日期和使用PHP date format()的对应格式。此格式在用户输入之前未知 我必须把这个日期转换成一个已知的格式,如“d/m/Y h:I:s”。 我的PHP服务器使用5.2.1 PHP版本,因此不能使用DateTime类 有人有一个替代DateTime::createFromFormat方法的方法,一个这样做的脚本,或者一个帮助我的想法 致以最良好的祝愿, 弗洛里安 编辑: 我编写了一个函数,用于将日期转换为其他格式,而不需要知道原始日期格式: /**
我有一个用户格式化的日期和使用PHP date format()的对应格式。此格式在用户输入之前未知 我必须把这个日期转换成一个已知的格式,如“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;
}
该函数可以满足您的所有需要
例如(从文件中):
然而,如果你不知道格式,这或多或少是一个粗略的猜测,你必须使用这样的东西,给你(希望)一个正确的日期,但可能会失败
例如(从文件中):
将打印:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
不完全确定您的意思,但在php中,您可以使用strotime函数尝试解密输入,即date('d/m/Y h:i:s',strotime('昨天');你能给我们一个你想转换的日期的例子吗?为什么你不能简单地使用
$tstamp=strotime($userdate)
然后date('d/m/Y h:i:s',$tstamp)
?对于@therao:日期是以已知格式提交的,需要解码成通用格式。因此,如果用户提交了2012年11月10日,并且他们在英国转换为MySQL 2012/11/10,格式为dd/mm/YYYY。但如果美国将日期转换为2012/10/11,格式为mm/dd/YYYY?如果以美国以外的格式提交日期,则strotime()将无法正常工作,例如,美国日期为mm/dd/YYYY,而英国日期为dd/mm/YYYY。如果你使用MySQL格式yyyy-mm-dd hh:ii:ss,那么它将更容易处理为strotime(),比较和数据库存储将更容易。他说,在用户输入之前,这种格式是未知的。你必须在评论中使用phpfreaks.com的替代品中的蜡笔,因为这是用于(php5>=5.3.0)@Waygood你是对的。谢谢你的提示。
print_r(date_parse("2006-12-12 10:00:00.5"));
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)