Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
日期\u从相当于PHP5.2(或更低版本)的\u格式创建\u_Php_Date - Fatal编程技术网

日期\u从相当于PHP5.2(或更低版本)的\u格式创建\u

日期\u从相当于PHP5.2(或更低版本)的\u格式创建\u,php,date,Php,Date,我在本地机器上使用PHP5.3,需要解析英国日期格式(dd/mm/yyyy)。我发现strotime不能使用那种日期格式,所以我使用了date\u create\u from\u format,效果很好 现在,我的问题是,我的登台服务器运行的是PHP5.2,而date\u create\u from\u format在该版本上不起作用。(这是一个共享服务器,不知道如何升级到PHP5.3) 那么,我是否可以使用类似于date\u create\u from\u format的函数?定制或PHP本机

我在本地机器上使用PHP5.3,需要解析英国日期格式(dd/mm/yyyy)。我发现strotime不能使用那种日期格式,所以我使用了
date\u create\u from\u format
,效果很好

现在,我的问题是,我的登台服务器运行的是PHP5.2,而
date\u create\u from\u format
在该版本上不起作用。(这是一个共享服务器,不知道如何升级到PHP5.3)


那么,我是否可以使用类似于
date\u create\u from\u format
的函数?定制或PHP本机?

如果只需要解析一种特定格式,它是基本的字符串操作

list($d,$m,$y)=explode("/",$datestr);

请尝试在PHP5.1及以上版本中可用的版本。

如果您无法使用
strtime
,那么这里有一个不同的想法。它与子母弹上校的方法类似,而是使用
sscanf
将日期部分值解析为变量,并使用这些变量构造新的
DateTime
对象

list($day, $month, $year) = sscanf('12/04/2010', '%02d/%02d/%04d');
$datetime = new DateTime("$year-$month-$day");
echo $datetime->format('r');
包括以下代码:

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();

使用DD-MM-YY格式和时间戳,我想这对您来说会更容易

$date="31-11-2015";
$timestamp=strtotime($date);
$dateConvert=date('d-m-Y', $timestamp);
echo $dateConvert;

我用过它

这只给了他三个字符串,而不是解析后的字符串date@stereofrog哦,真是个问题:)谢谢戴维。是的,我使用的是Windows for local dev(使用XAMPP、PHP5.3),但是临时服务器和实时服务器都是LAMP,所以这可以作为一个后备方案。谢谢你的想法。不幸的是,即使在我添加了从“s”到“%s”的映射之后,我也无法将其用于格式“Y-m-dh:I:s”。。。