php dateTime :: createFromFormat在5.2?

前端之家收集整理的这篇文章主要介绍了php dateTime :: createFromFormat在5.2?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在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升级的机会).

包括下一个代码
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();
原文链接:https://www.f2er.com/php/131213.html

猜你在找的PHP相关文章