0

I wonder if anyone could assist with this as I am not sure where to look.

I have an old site that runs on a shared server that I have little control over. I have a script that runs well on my other sites and want to add it to this older site however it doesn't display the time correctly. All my other sites run on PHP 5+ however this is the first php script on the old site which I have now discovered is running PHP 4.4.8.

So I have narrowed down the problem to a function within a larger script and think this is where the incompatibility lies. The code basically takes the data supplied and converts it into days, hours, minutes & seconds.

    function time_left ($e_time) {
  $e_time = strtotime($e_time);
  $e_time = substr($e_time, 0, 10);
  $time_stamp = explode("-", date('n-d-Y-H-i-s', $e_time));
  $e_time = mktime($time_stamp[3], $time_stamp[4], $time_stamp[5], $time_stamp[0], $time_stamp[1], $time_stamp[2]);
  $server_time = time();
  $diff_time = ($e_time - $server_time);
  if ($diff_time < 0) $diff_time = 0;
  unset($time_stamp);

  # get days remaining
  $time_stamp[1] = floor($diff_time/60/60/24);
  if($time_stamp[1] <= 0){$time_stamp[1] = "";} else {$time_stamp[1] .= "d ";}

  # get hours remaining
  $time_stamp[3] = floor(($diff_time - $time_stamp[1]*60*60*24)/60/60);
  if($time_stamp[3] <= 0){$time_stamp[3] = "";} else {$time_stamp[3] .= "h ";}

  # get minutes remaining
  $time_stamp[4] = floor(($diff_time - $time_stamp[1]*60*60*24 - $time_stamp[3]*60*60)/60);
  if($time_stamp[4] <= 0){$time_stamp[4] = "";} else {$time_stamp[4] .= "m ";}

  return $time_stamp[1] . $time_stamp[3] . $time_stamp[4];

}

Can anyone spot anything in the above code that you think may be incompatible in PHP 4.4?

Currently if I echo the raw time data variable it shows it in raw form but when it is put through the function above it displays nothing. As I say I don't have any issues with this coding on servers operating PHP 5+

Many thanks in advance for any help!

* Added

Ok I think I have isolated what the issue is and that is strtotime

The raw input that strtotime gets is this: 2013-03-18T21:38:58.000Z

At the moment because the script is running under PHP 4.4 if I echo this bit:

$e_time = strtotime($e_time);

It always returns a value of -124 but I am not sure why??

3
  • 4
    You are making a function named time which conflicts with a built in function named time. Even if this worked right, your function would be infinitely recursive (it calls time() on the 6th line.) Are you sure this is working PHP5? Commented Mar 18, 2013 at 20:26
  • @ColinMorelli haha nice one. i didn't see that. Commented Mar 18, 2013 at 20:28
  • Apologies have now edited to show correct version. Commented Mar 18, 2013 at 20:39

1 Answer 1

2

Remove

unset($time_stamp);

this line.

Not sure why you're unsetting it when you want to do operations on it right afterwards. Hope that helps

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

6 Comments

Not sure to be honest as it was written by someone else but will give your suggestion a try - many thanks.
what's a sample value for $e_time?
2013-03-18T21:38:58.000Z thats what appears if I echo the variable before putting it through the function
Don't see anything wrong to be honest. If error reporting is on and you don't see any errors, then there might be something wrong with the logic. I notice that if time() is about 6000 greater than the sample you gave me, the function returns a blank
Thanks psychobunny for your help - appreciated! No not seeing any errors with reporting on. Will keep looking
|

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.