在PHP 5.2中如何使用dateTime::createFromFormat?

10

我一直在使用 PHP 5.3 进行开发。

然而我们的生产服务器版本是 5.2.6。

我一直在使用

$schedule = '31/03/2011 01:22 pm'; // example input
if (empty($schedule))
    $schedule = date('Y-m-d H:i:s');
else {
    $schedule = dateTime::createFromFormat('d/m/Y h:i a', $schedule);
    $schedule = $schedule->format('Y-m-d H:i:s');
}
echo $schedule;

然而该函数在5.2中不可用。

在没有PHP升级的情况下,最简单的方法是什么?

6个回答

19

只需包含以下代码

function DEFINE_date_create_from_format()
  {

function date_create_from_format( $dformat, $dvalue )
  {

    $schedule = $dvalue;
    $schedule_format = str_replace(array('Y','m','d', 'H', 'i','a'),array('%Y','%m','%d', '%I', '%M', '%p' ) ,$dformat);
    // %Y, %m and %d correspond to date()'s Y m and d.
    // %I corresponds to H, %M to i and %p to a
    $ugly = strptime($schedule, $schedule_format);
    $ymd = sprintf(
        // This is a format string that takes six total decimal
        // arguments, then left-pads them with zeros to either
        // 4 or 2 characters, as needed
        '%04d-%02d-%02d %02d:%02d:%02d',
        $ugly['tm_year'] + 1900,  // This will be "111", so we need to add 1900.
        $ugly['tm_mon'] + 1,      // This will be the month minus one, so we add one.
        $ugly['tm_mday'], 
        $ugly['tm_hour'], 
        $ugly['tm_min'], 
        $ugly['tm_sec']
    );
    $new_schedule = new DateTime($ymd);

   return $new_schedule;
  }
}

if( !function_exists("date_create_from_format") )
 DEFINE_date_create_from_format();

这是一个非常好的答案。应该得到更多的赞!谢谢 - andreapier
支持 DateTime 的 'M':$schedule_format = str_replace(array('M', 'Y', 'm', 'd', 'H', 'i', 'a'),array('%b', '%Y', '%m', '%d', '%I', '%M', '%p'), $dformat); - kenorb
有一个小错误,因为'H'应该被替换为% H(24小时格式),而不是% I(12小时格式)。因此,这是改进后的代码行:$schedule_format = str_replace(array('M', 'Y', 'm', 'd', 'H', 'i', 'a'), array('%b', '%Y', '%m', '%d', '%H', '%M', '%p'), $dformat); - kenorb
无法在Windows上运行!_strptime_未实现。 - Marco Andreolli

10
因为strtotime在处理D/M/Y时表现不佳,而date_create_from_format不可用,所以strptime可能是你唯一的希望。它做了一些相当老派的事情,比如将年份视为自1900年以来的年数,并将月份视为一月为零月。下面是一些可怕的示例代码,使用sprintf将日期重新组合成DateTime可以理解的格式:
$schedule = '31/03/2011 01:22 pm';
// %Y, %m and %d correspond to date()'s Y m and d.
// %I corresponds to H, %M to i and %p to a
$ugly = strptime($schedule, '%d/%m/%Y %I:%M %p');
$ymd = sprintf(
    // This is a format string that takes six total decimal
    // arguments, then left-pads them with zeros to either
    // 4 or 2 characters, as needed
    '%04d-%02d-%02d %02d:%02d:%02d',
    $ugly['tm_year'] + 1900,  // This will be "111", so we need to add 1900.
    $ugly['tm_mon'] + 1,      // This will be the month minus one, so we add one.
    $ugly['tm_mday'], 
    $ugly['tm_hour'], 
    $ugly['tm_min'], 
    $ugly['tm_sec']
);
echo $ymd;
$new_schedule = new DateTime($ymd);
echo $new_schedule->format('Y-m-d H:i:s');

如果它正常运行,你应该看到相同且正确的日期和时间打印两次。

这可能是最好的解决方案,除非您可以更好地控制输入并确保使用支持的日期和时间格式 - Jacob
strtotime 不支持 D/M/Y 格式,只能处理 Y/M/D 和 M/D/Y 格式。如果你尝试传递一个 D/M/Y 格式的日期给它,它会失败。strtotime('20/02/2003') 返回 false。你需要传递 '20.03.2003'(注意点号!)才能让它识别这种格式,但这并不是你期望的日期格式。 - Charles
我知道strtotime不喜欢/,但是如果你把/转换成-,它就可以完美地工作了。我发现很难阅读你上面的代码,请注释一下以解释每个函数正在做什么? - Hailwood
看起来支持 D-M-Y,但你的示例日期只包含斜杠。此外,虽然支持 D-M-Y,但不支持 M-D-Y -- 在 M/D/Y 上替换斜杠会导致无法解析的日期。我稍后会在我的代码中添加更多注释。 - Charles
@Hailwood @Charles 请看一下我的回答 https://dev59.com/KG435IYBdhLWcg3wqB8x#5350457 关于日期格式。 - Jacob
显示剩余4条评论

6

我认为更加清晰的方法是扩展DateTime类并自己实现createFromFormat(),如下所示:

class MyDateTime extends DateTime
{
    public static function createFromFormat($format, $time, $timezone = null)
    {
        if(!$timezone) $timezone = new DateTimeZone(date_default_timezone_get());
        $version = explode('.', phpversion());
        if(((int)$version[0] >= 5 && (int)$version[1] >= 2 && (int)$version[2] > 17)){
            return parent::createFromFormat($format, $time, $timezone);
        }
        return new DateTime(date($format, strtotime($time)), $timezone);
    }
}

$dateTime = MyDateTime::createFromFormat('Y-m-d', '2013-6-13');
var_dump($dateTime);
var_dump($dateTime->format('Y-m-d'));

这将适用于 PHP >= 5.2.0 的所有版本。

在此处查看演示http://3v4l.org/djucq


如果使用像“d/m/Y”这样的非常规英语格式,它在5.2.x中无法正常工作,但是如果您将“/”替换为“-”,它就可以正常工作。 - morgar
由于5.2.x版本已经在近6年前到达EOL,所以我并不太担心它。无论如何,还是谢谢你让我知道了。 - vascowhite

0

由于这并没有展示如何使用“z”选项将YYYY:DDD:HH:MM:SS时间转换为Unix秒,因此您必须创建自己的函数来将DOY转换为月份和日期。这就是我所做的:

function _IsLeapYear ($Year)
{
    $LeapYear = 0;
    # Leap years are divisible by 4, but not by 100, unless by 400
    if ( ( $Year % 4 == 0 ) || ( $Year % 100 == 0 ) || ( $Year % 400 == 0 ) ) {
        $LeapYear = 1;
    }
    return $LeapYear;
}

function _DaysInMonth ($Year, $Month)
{

    $DaysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    return ((_IsLeapYear($Year) && $Month == 2) ? 29 : $DaysInMonth[$Month - 1]);
}

function yydddhhssmmToTime($Year, $DOY, $Hour, $Min, $Sec)
{
   $Day = $DOY;
   for ($Month = 1;  $Day > _DaysInMonth($Year, $Month);  $Month++) {
    $Day -= _DaysInMonth($Year, $Month);
   }

   $DayOfMonth = $Day;

   return mktime($Hour, $Min, $Sec, $Month, $DayOfMonth, $Year);
}

$timeSec = yydddhhssmmToTime(2016, 365, 23, 23, 23);
$str = date("m/d/Y H:i:s", $timeSec);
echo "unix seconds: " . $timeis . " " . $str ."<br>";

页面上的输出显示它的工作原理,因为我可以将秒数转换回原始输入值。 Unix 秒数:1483140203 2016年12月30日23:23:23


-1

仅日期和时间

$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:s', '2015-04-20T18:56:42');

ISO8601无冒号

$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sO', '2015-04-20T18:56:42+0000');

带有冒号的ISO8601

$date = $dateTime->format('c');

Salesforce ISO8601格式
DateTime::createFromFormat('Y-m-d\TH:i:s.uO', '2015-04-20T18:56:42.000+0000');

希望这能为某人节省时间!


只是看了问题,对不起,本来想发布到适用的问题上,但很兴奋终于解决了问题,不知道如何删除这个帖子,我的错。 - Shelly Warren

-1
$your_datetime_object=new DateTime($date);
$date_format_modified=date_format($your_datetime_object,'D M d Y');//Change the format of date time

我在5.2的生产服务器上遇到了类似的问题,所以我使用了上述日期时间来创建一个对象,然后按照我的喜好更改了格式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接