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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.