2011-09-24 117 views
0

我想從一個PHP文件中包含一個函數到另一個函數中,並使用帶有參數的函數並獲取返回值。一切都很好,直到我運行功能,PHP死亡。我怎樣才能解決這個問題?PHP - 爲什麼包含來自其他文件的函數失敗?

timestamp_reader.php:

<?php 
function get_time($timestamp){ 
    $year = substr($timestamp, 0, 4); 
    $month = substr($timestamp, 4, 2); 
    if(substr($month, 0, 1) == "0") 
     $month = substr($month, 1, 1); 
    $day = substr($timestamp, 6, 2); 
    if(substr($day, 0, 1) == "0") 
     $day = substr($day, 1, 1); 
    $hour = substr($timestamp, 8, 2); 
    if(substr($hour, 0, 1) == "0") 
     $hour = substr($hour, 1, 1); 
    $hour = intval($hour); 
    if($hour > 12){ 
     $hour = $hour - 12; 
     $pm = true; 
    }else $pm = false; 
    $minute = substr($timestamp, 10, 2); 
    if(substr($day, 0, 1) == "0") 
    $day = substr($day, 1, 1); 
    if($pm) $minute .= " PM"; 
    else $minute .= " AM"; 

    return $month . "/" . $day . "/" . $year . " " . $hour . ":" . $minute; 
} 
?> 

而且我希望獲得此功能的文件(注意,這是在不同的目錄):

...some PHP code before this... 
include "/project/includes/timestamp_reader.php"; 
while($row=mysql_fetch_array($result)){ 
    $msg = $row['message']; 
    $timestamp = $row['timestamp']; 
    $time = get_time($timestamp); 
    echo "<p><center>" . $time . " " . $msg . "</center></p>"; 
} 

我想這個數字並將其用於各種功能,所以如果可能的話,我不必每次都輸入它們。另外,我需要類似的東西來創建項目範圍的變量。

無論如何,感謝您的幫助!

+3

你會得到什麼錯誤? – Niko

+0

什麼是錯誤信息? –

+3

看起來像使用'date()'和'strtotime()'在這裏會是一個更好的主意。 –

回答

0

爲什麼不使用PHP中的date()function

例如,

$timestamp = "1316922240"; 
echo date("m-j-Y", $timestamp); 

將根據右現在時間戳打印出2011年9月24日 - 如果時間戳是從昨日起,日期回聲版將是昨天。

0

嘗試require(),如果路徑錯誤,則失敗,我敢打賭是這種情況。

+1

由於包含一個帶有函數的文件,在這種情況下'require_once()'會更好。 – Niko