2010-03-31 89 views
1

可能失去了一些東西簡單:PHP的日期格式

<?php 
$con=mysql_connect('localhost','xxx','xxx'); 

$db=mysql_select_db('xxx'); 

$sql = mysql_query("SHOW TABLE STATUS WHERE `name`=\"my_table\""); 

$foo=mysql_fetch_array($sql); 

return $foo['Update_time']; 

// returns 2010-03-31 16:51:06 
?> 

我如何格式化這個。我試着用

date('l jS F Y @ H:i:s', $foo['Update_time']); 

但這返回:

1日1970年1月@一點33分30秒

大概爲日期()預計時間戳。

thanks..im相信這是很明顯的,但我的頭腦一片空白

回答

3

date函數期望的UNIX Timestamp作爲其第二個參數 - 而不是包含日期的字符串。

這意味着你必須到字符串到時間戳轉換 - 這可以使用strtotime函數來完成:

$ts = strtotime($foo['Update_time']); 
echo date('l jS F Y @ H:i:s', $ts); 


注1:YYYY-MM-DD HH-MM-SS格式深受strtotime瞭解;這在處理來自MySQL的日期時通常很有幫助。

注2:使用datestrtotime,其時間戳工作(reprenseted爲32位整數,因爲1970-01-01計數的秒數),你將被限制在1970年和2038年的日期;如果您需要使用該範圍以外的日期,則必須使用DateTime class

+0

感謝的是,是有道理的。 – calum 2010-03-31 18:21:37

+0

不客氣:-) – 2010-03-31 18:31:20

3

這應該工作:

date('l jS F Y @ H:i:s', strtotime($foo['Update_time'])); 

strtotime()將日期字符串,並把它變成一個時間戳,然後你可以使用date()

+0

知道這是簡單的,謝謝你! – calum 2010-03-31 18:20:59

0

您需要使用strtotime功能:

date('l jS F Y @ H:i:s', strtotime($foo['Update_time']));