2016-10-02 81 views
0

我有以下的表,我正確地得到了所有信息,但在「日期」出現在一個塊格式:PHP日期和時間格式

<?php 
    // search for desired team 
    $searchQuery = $api->searchTeam(urlencode("Real Madrid")); 
    // var_dump searchQuery and inspect for results 
    $response = $api->getTeamById($searchQuery->teams[0]->id); 
    $fixtures = $response->getFixtures('home')->fixtures; 
?> 

<tr> 
    <td><?php echo $fixture->homeTeamName; ?></td> 
    <td>-</td> 
    <td><?php echo $fixture->awayTeamName; ?></td> 
    <td><?php echo $fixture->result->goalsHomeTeam; ?></td> 
    <td>:</td> 
    <td><?php echo $fixture->result->goalsAwayTeam; ?></td> 
    <td><?php echo $fixture->date; ?></td> 
</tr> 

結果:

Home Team   Away Team   Result  Date/Time 
Real Madrid CF - RC Celta de Vigo 2 : 1  2016-08-27T18:15:00Z 

如何添加空間在日期和時間之間刪除T和Z字符,而我沒有訪問php.ini文件?

+1

請參閱['str_replace'](http ://php.net/manual/en/function.str-replace.php)和相關的[String Functions](http://php.net/manual/en/ref.strings .php) –

回答

2

您應該使用date功能與strtotime。這將允許您根據需要設置字符串的格式。例如:

echo date('Y-m-d h:i:s', strtotime('2016-08-27T18:15:00Z')); 

注意h這裏是一個12小時制。對於24小時制格式,請使用G

演示:https://eval.in/653840

http://php.net/manual/en/function.date.php的不同日期字符。

或者,您可以使用str_replace但這隻會取代t s和z s。

echo str_replace(array('T', 'Z'), array(' ', ''), '2016-08-27T18:15:00Z'); 

演示:https://eval.in/653838

2

看看date() function,它提供了許多定製的可能性。

對於您可以選擇寫,例如目前的情況:

<td><?php echo date('m/d/Y h:i A', $fixture->date); ?></td> 
+1

注'2016-08-27T18:15:00Z'不是時間戳,所以您需要將其轉換。 – chris85

+0

@cFreed這看起來不錯,但我得到了奇怪的日期:01/01/1970 04:33 AM – Isabella

+1

@ chris85我同意_if它不是timestamp_它必須首先轉換。但是,如果OP中沒有任何信息,你怎麼知道它目前是什麼? – cFreed

0

根據@cFreed,@ chris85和其他捐助的答案,我得到它的工作是這樣的:

echo str_replace(array('T', 'Z'), array(' ', ''), $fixture->date); 

結果: 2016-08- 27 18:15:00

1

你的日期不是一個時間戳,所以你可以使用字符串操作來刪除T a第二ž通過創建一個簡單的功能,你可以在未來使用..

function replace_str($originastring,$valuetoreplace,$valueasreplacement) 
{ 
    if(isset($originalstring)&&isset($valuetoreplace)&&isset($valueasreplacement)) 
    { 
     if(is_array($valuetoreplace)&&is_array($valueasreplacement)&&($valutoreplace.length==$valueasreplacement.length)) 
     { 
      for($i=0;$i<$valuetoreplace.length;$i++) 
      { 
       $originalstring=str_replace($originalstring,$valueasreplacement[i]); 
      } 
     } 
     else 
     { 
     $originalstring=str_replace($originalstring,$valueasreplacement[i]); 
     } 
     //any other stuff 
     return $str1; 
    } 
} 

,並在你的代碼

<td><?php echo replace_str($fixture->date,array('T','S'),array('','')); ?></td> 

注:避免直接修改變量的值(我的意思是,爲什麼API可能在你的日期中添加z和T可能有一些目的,所以當你執行這樣的替換時,一定要保持原始值不變,以防萬一你需要原始值..:D)

+0

它沒有這個功能,謝謝。 – Isabella

+1

高興地幫助...如果您需要任何額外的修改,您可以在這樣一個功能...:D – RohitS