eevan79 Posted September 16, 2010 Share Posted September 16, 2010 I want to users can setup time difference in hours, but I dont know how to solve this. Here is script for localtime: $date_format = 'd.m.Y H:i'; $timezone = new DateTimeZone("Europe/Berlin"); $date = new DateTime(); $date->setTimezone($timezone); echo "<div align='right'> Today is " . $date->format("$date_format") . "</div>"; Now I have $user_time in config and users can change this value (example: +1 hours, -3 hours etc...). Users time settings ($user_time) are saved in database. How to display time based on user_time (ex. $date->format("$date_format") ) to display message like this: Today is 16.09.2010 18 (+$user_time) : 40 Quote Link to comment https://forums.phpfreaks.com/topic/213583-time-date-problem-adding-custom-hours/ Share on other sites More sharing options...
Psycho Posted September 16, 2010 Share Posted September 16, 2010 Without knowing the functionality of your class "DateTime()" I'm not sure exactly where the change could be implemented. But, a simple solution based upon the dat ayou have would be to use strtotime(). http://us.php.net/manual/en/function.strtotime.php With that function you can convert a textual representation of a date/time into a timestamp. Plus, it allows you to do some ad-hoc addition/subtraction. So, if you ahve the current datetime in a string (not a timestamp) you can use the value of $user_time to create a timestamp offset per the user's setting. Quote Link to comment https://forums.phpfreaks.com/topic/213583-time-date-problem-adding-custom-hours/#findComment-1111763 Share on other sites More sharing options...
eevan79 Posted September 16, 2010 Author Share Posted September 16, 2010 Thanks for reply. I already tried before function strtotime, and obviously something is wrong with my code. Finally, I have founded solution with strtotime function: echo "<div align='right'> $l_today_is " . date($date_format,strtotime($date->format("$date_format")." $user_time hours")); Quote Link to comment https://forums.phpfreaks.com/topic/213583-time-date-problem-adding-custom-hours/#findComment-1111818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.