Using javascript I know that my users timezone is UTC +3.
Now I want to create DateTime object with this knowledge:
$usersNow = new DateTime('now', new DateTimeZone("+3"));
I receive as a respsonse:
'Unknown or bad timezone (+2)'
What am I doing wrong? How can I fix?
DateTimeZone requires a timezone not an offest
Modern answer:
Documentation:
For anyone who comes across this, I was facing the same problem, so in the end I extended the DateTime class and overrode the
__construct()
method to accept an offset (in minutes) instead of a timezone.From there, my custom
__construct()
works out what the offset is in hours and minutes (e.g. -660 = +11:00) and then usesparent::__construct()
to hand my date, custom formatted to include my offset, back to the original DateTime.Because I'm always dealing with UTC times in my application, my class also modifies the UTC time by subtracting the offset, so passing Midnight UTC and an offset of -660 will show me 11am
My solution is detailed here: https://stackoverflow.com/a/35916440/2301484
did you try this
http://php.net/manual/en/function.strtotime.php