Sefu
Sefu

Reputation: 2494

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. 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!

Upvotes: 0

Views: 136

Answers (3)

MrJ
MrJ

Reputation: 1938

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.

Upvotes: 0

tereško
tereško

Reputation: 58444

How about you avoid reinventing a wheel ? .. especially so misshapen one :

Upvotes: 1

3on
3on

Reputation: 6339

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

Upvotes: 0

Related Questions