2016-10-02 85 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