PHP - 为什么包含另一个文件中的函数会失败?

发布于 2024-12-06 03:08:36 字数 1389 浏览 0 评论 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>";
}

我想弄清楚并将其用于各种函数,这样我就不必键入它们如果可能的话,每次都会。另外,我需要类似的东西来创建项目范围的变量。

无论如何,感谢您的任何帮助!

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. All is good until I run the function, where PHP dies. How can I fix this?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是年少的欢喜 2024-12-13 03:08:36

为什么不在 PHP 中使用 date() 函数

例如,

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

将根据现在的时间戳打印出 09-24-2011 - 如果时间戳是昨天的,则回显的日期将是昨天。

Why not use the date() function within PHP?

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.

是你 2024-12-13 03:08:36

尝试使用 require() ,如果路径错误,它会失败,我敢打赌就是这种情况。

try with require(), it fails if the path is wrong, which I bet is the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文