Jump to content

Time date problem (adding custom hours)


eevan79

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/213583-time-date-problem-adding-custom-hours/
Share on other sites

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.

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"));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.