2011-03-22 46 views
9

我一直在開發PHP 5.3。php dateTime :: createFromFormat在5.2中?

但是我們的生產服務器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升級的機會),最簡單的方法。

回答

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

這是一個很好的答案。應該真的得到更多的讚揚!感謝 – andreapier 2013-03-15 20:45:33

+0

支持日期時間 'M':'''$ schedule_format = str_replace函數(陣列( 'M', 'Y', 'M', 'd', 'H', 'I', 'A'),陣列( '%b', '%Y', '%M', '%d', '%I', '%M', '%p'),$ DFORMAT);''' – kenorb 2013-09-09 15:59:54

+0

有一個小缺陷,因爲'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 2013-09-19 08:40:25

9

由於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'); 

如果一切正常,你應該看到打印兩次相同的,正確的日期和時間。

+0

出於好奇,你的代碼和'else {$ {schedule = str_replace('/',' - ',$ schedule)之間的區別是什麼? $ schedule = date('Y-m-d H:i:s',strtotime($ schedule)); }' – Hailwood 2011-03-22 23:50:00

+0

這可能是最好的解決辦法,除非你可以通過輸入更多的控制權,並確保您使用[支持的日期和時間格式(http://www.php.net/manual/en/datetime.formats。 PHP)。 – Jacob 2011-03-22 23:54:06

+0

'strtotime'不瞭解D/M/Y。它只能處理Y/M/D和M/D/Y。如果您嘗試將D/M/Y傳遞給它,它將會失敗。 'strtotime('20/02/2003')'返回'false'。你必須通過20.03.2003''(注意點!)才能識別該格式,這不是你期望的日期格式。 – Charles 2011-03-22 23:55:44

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

+0

這個工程'FANTASTIC'謝謝! ! – degenerate 2015-06-29 19:09:23

+0

這在5.2.x中不起作用,像'd/m/Y'這樣的非英語格式,但是如果用' - '替換'/',它就會起作用。 – morgar 2016-10-14 01:39:39

+0

由於近6年前5.2.x版本已經發布[EOL](http://php.net/eol.php),我並不太在意。無論如何,謝謝你讓我知道。 – vascowhite 2016-10-14 06:08:32

-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已經與生產服務器類似的問題,所以我就用上面的日期時間來創建一個對象,然後格式更改爲我喜歡如上。

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

希望這樣可以節省時間的人!

+0

剛剛閱讀這個問題,請原諒我,打算在適用的問題上發佈,但很高興終於解決了這個問題,並不知道如何刪除這個,我的壞。 – 2015-05-05 19:01:55

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 seconds:1483140203 12/30/2016 23:23:23

相關問題