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)。

4

6 回答 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();
于 2012-08-25T16:18:51.543 回答
10

因为strtotime在面对 D/M/Y 时表现不佳并且date_create_from_format无法使用,strptime可能是您在这里唯一的希望。它做了一些非常老派的事情,比如将年份视为自 1900 年以来的年数,将月份视为 1 月为零月。这是一些可怕的示例代码,用于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');

如果它有效,您应该会看到两次打印的相同、正确的日期和时间。

于 2011-03-22T23:44:50.627 回答
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

于 2013-11-17T17:54:34.943 回答
0

Since this is not really showing how to convert YYYY:DDD:HH:MM:SS time to unix seconds using the "z" option you have to create you own functions to convert the DOY to month and day of month. This is what I did:

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>";

The output on the page shows its working since I can convert back the seconds back to the original input values. unix seconds: 1483140203 12/30/2016 23:23:23

于 2015-12-30T21:15:36.360 回答
-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 的生产服务器上遇到了类似的问题,所以我使用上面的 datetime 创建一个对象,然后将格式更改为我喜欢的格式。

于 2014-01-20T00:31:48.990 回答
-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');

希望这可以节省一些时间!

于 2015-05-05T18:04:14.913 回答