0

So, I have the following mysql time format:

date_default_timezone_set('Canada/Mountain');
$date = date('Y-m-d H:i:s', time());
'post_data' => $date,

Giving "2016-01-25 13:26:07" format.

To change its format to "ago" I used the following:

//Time to ago
function ago($time)
{
   $periods = array("second", "min", "hr", "day", "week", "month", "year", "decade");
   $lengths = array("60","60","24","7","4.35","12","10");
   $now = time();
   $difference     = $now - $time;
   $tense         = "ago";
   for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       $difference /= $lengths[$j];
   }
   $difference = round($difference);
   if($difference != 1) {
      $periods[$j].= "s";
    }

    return "$difference $periods[$j]";
  }

However, all the new row input gets "7hr" as its time when it should be "1 second" (as it is a new input).

Any suggestions to how to fix this?

Thanks

Steve Kim
  • 5,293
  • 16
  • 54
  • 99
  • 1
    My suggestion would be to dump your results after each step and debug.. – Kamil Gosciminski Jan 25 '16 at 20:41
  • This seems to do exactly what you want: http://stackoverflow.com/a/2916189/388476 – glennv Jan 25 '16 at 20:48
  • Do you find it all curious that the offset of Canada/Mountain time from UTC is exactly seven hours? I'm just guessing here. But I suspect that the value you are inserting into MySQL is being interpreted as if its UTC rather than Canada/Mountain. I don't think it's the code you've posted here that is the problem. – spencer7593 Jan 25 '16 at 21:00
  • That's what I was thinking as well. I have been doing some research on how to go about. I think that is exactly it. – Steve Kim Jan 25 '16 at 21:01
  • MySQL: `show variables like 'time\_zone'`. – spencer7593 Jan 25 '16 at 21:02
  • I am not sure if I follow your instruction. :P – Steve Kim Jan 25 '16 at 21:05

0 Answers0