[英]PHP - Why does including a function from another file fail?
I am trying to include a function from one PHP file into another and use the function with a parameter and get a return value. 我试图将一个PHP文件中的函数包含到另一个文件中,并将该函数与参数一起使用并获取返回值。 All is good until I run the function, where PHP dies.
直到我运行PHP终止的函数之前,一切都很好。 How can I fix this?
我怎样才能解决这个问题?
timestamp_reader.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;
}
?>
And the file that I want access to this function (note, it is in a different directory): 和我要访问此功能的文件(请注意,它位于其他目录中):
...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>";
}
I'd like to figure this out and use it for a variety of functions so I don't have to type them in every time if possible. 我想弄清楚这一点并将其用于各种功能,因此,如果可能的话,我不必每次都键入它们。 Also, I need something similar for creating project-wide variables.
另外,我需要类似的东西来创建项目范围的变量。
Anyway, thanks for any and all help! 无论如何,感谢您的所有帮助!
How about you avoid reinventing a wheel ? 您如何避免重新发明轮子呢? .. especially so misshapen one :
..特别是畸形的:
尝试使用require(),如果路径错误,它将失败,我敢肯定是这样。
Why not use the date()
function within PHP? 为什么不在PHP中使用
date()
函数 ?
For example, 例如,
$timestamp = "1316922240";
echo date("m-j-Y", $timestamp);
Will print out 09-24-2011 based on that time stamp for right now - if the timestamp was from yesterday, the date echo-ed will be yesterday. 现在将根据该时间戳打印2011年9月24日-如果时间戳记为昨天,则回显日期为昨天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.