加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php dateTime :: createFromFormat在5.2?

发布时间:2020-12-13 13:13:29 所属栏目:PHP教程 来源:网络整理
导读:我一直在php 5.3开发. 但是我们的生产服务器是5.2.6. 我一直在使用 $schedule = '31/03/2011 01:22 pm'; // example inputif (empty($schedule)) $schedule = date('Y-m-d H:i:s');else { $schedule = dateTime::createFromFormat('d/m/Y h:i a',$schedule);
我一直在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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读