-2

I need to add +7 hours to a timestamp I'm getting from the DB, but I can't change it to unix because I'm using a function to convert it to "2 hours ago" and stuff like that.

How would I go about doing this? Every question I've seen uses strtotime() which doesn't work with the function I'm using.

Here's the function:

function time_passed($datetime, $full = false) {
    $datetimes = $datetime;
    $now = new DateTime;
    $ago = new DateTime($datetimes);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
   );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

So I go through this function with for example 2014-09-11 05:14:12 and it outputs 12 hours ago - Although I want it to output 19 hours ago

impedans
  • 3
  • 4
  • 1
    Show us sample data and what you've tried – John Conde Sep 11 '14 at 17:38
  • @JohnConde Yes, one second – impedans Sep 11 '14 at 17:39
  • can't you just convert it to a unix timestamp and then convert it back to whatever format you need for your function? And what format is the "timestamp" you are getting? – Jonathan Kuhn Sep 11 '14 at 17:39
  • Why are you trying to add +7 hours? Looks like you are trying to substitute for time zone difference. Why not just declare your `$now` variable with the timezone setting and let php do the magic for you? `new DateTime("now", new DateTimeZone("America/Los_Angeles"));` or whatever your timezone is. You can find a list of timezones at http://php.net/timezones – Jonathan Kuhn Sep 11 '14 at 17:53
  • That doesn't change the output unfortunately. I tried both adding your line of code (changing it to Europe/Oslo) in the connect file on a new line, and replacing the line in the time_passed function @JonathanKuhn – impedans Sep 11 '14 at 17:59
  • I believe the timezone is set in the database, but I use Godaddy so I can't change it. I've been searching and googling for hours and the only thing I've found is that I should do something like this. – impedans Sep 11 '14 at 18:01
  • @JonathanKuhn I did as you said and converted it back after adding. This worked, if you would leave it as an answer I would check it as correct. – impedans Sep 11 '14 at 18:11
  • I was replying when your comment came through. I was going to just suggest correcting the `$now` time yourself by adding 7 hours to that. `$now->add(new DateInterval("PT7H"));` on the line right after you declare `$now` – Jonathan Kuhn Sep 11 '14 at 18:13

1 Answers1

0

Why are you trying to add +7 hours? Looks like you are trying to substitute for time zone difference. Why not just declare your $now variable with the timezone setting and let php do the magic for you? new DateTime("now", new DateTimeZone("America/Los_Angeles")); or whatever your timezone is. You can find a list of timezones at http://php.net/timezones

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43