1

i am writing a time since function to return the time since a given mysql datetime. When taking the $oldtime from current time() it is returning a negative int when i need a positive int. I have written similar functions before in other languages but i have become blind to this problem, so any help would be much appreciated.

function timeSince($time){
        $today = date("Y");
        $oldtime = strtotime($time);
        $time = time() - $oldtime;
        $tokens = array (
            3600 => 'h',
            60 => 'm',
            1 => 's'
        );

        if($time >= 86400){
        }
    }

echo timeSince('2016-02-25 14:35:00');
  • For me, your example should have a negative value for $time because it isn't 2:35pm on Feb 25, 2016 yet. – kainaw Feb 25 '16 at 18:28

3 Answers3

3

it could be much more convenient if you use PHP's DateTime and DateInterval classes and their methods:

function timeSince($datetime) {
    $now        = strtotime("now");
    $then       = strtotime($datetime);
    $dt_now     = new DateTime("@" . $now);
    $dt_then    = new DateTime("@" . $then);

    //DateTime's diff method returns a DateInterval object which got a format method:
    return $dt_now->diff($dt_then)->format('%a days, %h hours, %i minutes and %s seconds');
}


some test cases:

//my local date & time is around "2016-02-25 19:49:00" when testing
echo '<pre>';

echo timeSince('2016-02-25 19:30:00');
//0 days, 0 hours, 19 minutes and 11 seconds
echo PHP_EOL;

echo timeSince('2013-11-02 15:43:12'); 
//845 days, 4 hours, 4 minutes and 3 seconds
echo PHP_EOL;

echo timeSince('2017-01-31 00:22:45'); 
//340 days, 4 hours, 35 minutes and 30 seconds
echo PHP_EOL;

echo timeSince('1950-05-14 07:10:05');
//24028 days, 12 hours, 37 minutes and 10 seconds
echo PHP_EOL;


code partially based on this answer: https://stackoverflow.com/a/19680778/3391783

Community
  • 1
  • 1
low_rents
  • 4,481
  • 3
  • 27
  • 55
  • Thanks for your help, although the answer to my specific problem was answered below i wanted to say that i agree with your comment, a much easier solution. – user2784408 Feb 25 '16 at 19:08
2

strtotime uses timezone in your PHP settings. Depending on timezone set, it might convert to the time that is yet to happen. For example, on my ukrainian server, strtotime('2016-02-25 14:35:00') converts to 1456403700, on a server in another timezone (US/Pacific) it converts to 1456439700.

Quote from PHP docs:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

You can add UTC/GMT offset to your datetime (1st param), for example strtotime('2016-02-25 14:35:00 +0800') or ('2016-02-25 14:35:00 GMT+08:00') will convert to 1456382100

Community
  • 1
  • 1
Igor Yavych
  • 4,166
  • 3
  • 21
  • 42
  • Thanks for the reply Igor, this may be the problem as i have used similar code before and not come across this problem. I will test it out and report back. – user2784408 Feb 25 '16 at 18:49
0

In your example, $oldtime must be smaller value than current time().

So, if you want to count time between larger value, simply reverse your equation:

This line: $time = time() - $oldtime;

Becomes: $time = $oldtime - time();

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
  • 2
    Or, use `abs($time()-$oldtime);` – kainaw Feb 25 '16 at 18:32
  • Thanks for the reply, as far as i was aware time() would always return a larger number as it is the most recent,which is why i am having such a problem with it. I have seen it done similar to this in other stack queries. Is my thinking wrong? Cheers. – user2784408 Feb 25 '16 at 18:38