0

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!

5
  • and what is the error message ? Commented Sep 24, 2011 at 20:55
  • 3
    Looks like using date() and strtotime() would be a better idea here. Commented Sep 24, 2011 at 20:55
  • What's the error message? PS, it'd probably be neater to do the time formatting in SQL in your SELECT statement rather than in PHP. (assuming you're using MySql: dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html ) Commented Sep 24, 2011 at 20:56
  • Firefox tells me Error in parsing value for 'filter'. Declaration dropped. in error console. Commented Sep 24, 2011 at 20:58
  • 1
    @Richard , what has firefox's console to do with php errors ? Commented Sep 24, 2011 at 21:05

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

1 Comment

As you are including a file with a function, require_once() would be preferable in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.