Adithya
Adithya

Reputation: 183

Set time offset in php

Is there some way to return all date returned by the date() function to be offset by some specific amount of time ? I tried date-default-timezone-set(), didn't seem to work on the server, so is there any other way?

Upvotes: 0

Views: 1575

Answers (1)

Jochen Van de Velde
Jochen Van de Velde

Reputation: 96

You could also write your own version of the date() function which returns the date processed by the callback you require (adding an offset).

<?php

function date_offset($format, $offset)
{
  $time = time() + $offset; # $offset should be in milliseconds
  return date($format, $time); # with your require display format $format
}

If you really want to just call date(), i.e. for sake of consistency, you could write your app in your own namespace and simply name your custom function date(). In that case, PHP's original date() function has to be called as \date().

Upvotes: 2

Related Questions