changing server time to my time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mike
Forum Newbie
Posts: 8
Joined: Mon Jul 15, 2002 4:18 pm

changing server time to my time

Post by mike »

Hi....... me server time is 5 hours behind me (i.e when i post news it says time is 5am instead of 10am) im sure I rerad of a way to change this but for the life of me I can not find it any more!

all help much appreciated....

mike
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

what are you using to calculate the date? if you enter it manually, you'll just need to enter it with the 5 hour variation. otherwise if you are using something like date() or now() within a mysql query, it will use whatever the local time on the server is.
mike
Forum Newbie
Posts: 8
Joined: Mon Jul 15, 2002 4:18 pm

i am using getdate()

Post by mike »

i am using getdate() so is their no way of it adding 5 hours to what the getdate recieves?

thanks

mike
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Code: Select all

$today = getdate(); 
$hour = $todayї'hours'] + 5;
if($hour > 23)
    $hour = $hour - 24;
echo $hour;
mike
Forum Newbie
Posts: 8
Joined: Mon Jul 15, 2002 4:18 pm

thanks but....

Post by mike »

ok that works very well mate but.....
when the time is pushed 5 hours forward and into the next day the date isnt moving forward one--- how do i make it do that?-- this is the code as it is now (which works :))

$date = getdate();
$mday = $date['mday'];
$mon = $date['mon'];
$year = $date['year'];
$today = getdate();
$hour = $today['hours'] + 5;
if($hour > 23)
$hour = $hour - 24;
echo $hour;
$minutes = $date['minutes'];

thanks for all help
mike
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

That's where you get to do the fun part of programming. If you move the hour back 23, move the day up 1. If the day was 31, it's now 1 - figure out the logic. Practice this type of simple logic, and the simple things will start to come easier. Peace
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

actually, the easiest thing would probably be to convert the date to "seconds since epoch" using mktime(). then add 1800 (60 * 60 * 5), and then convert it back to whatever format you want using date(). this way the date function will handle all of the dirty work (last day of month, etc).
mike
Forum Newbie
Posts: 8
Joined: Mon Jul 15, 2002 4:18 pm

er.....

Post by mike »

guys you both completely lost me their :S

I understand what you are saying but have no idea what so ever how to put that into code!
thats why I am here!

mike
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

ok, if you're just using the current time (plus 5 hours of course), then try this....

Code: Select all

// get UNIX timestamp (number of seconds since epoch)
$current_time = time();

// add 5 hours.  5 hours * 60 minutes * 60 seconds = 18000
$adjusted_time = $current_time + 18000;

// at this point, it depends on your database field type.  if you have date
// field, you would use this
$formatted_time = date('Y-m-d', $adjusted_time);

// if you are storing the epoch formatted time in the database (such as
// in a bigint field), then you don't need to change anything
$formatted_time = $adjusted_time;


// then just insert this into your database
mysql_query("INSERT into news SET date=$formatted_time");

let me know if any of that is unclear.
Post Reply