Link to home
Start Free TrialLog in
Avatar of way_ching
way_ching

asked on

Convert time to one in different time zone in PHP

I am wondering how to convert a given time to the ones in different time zone.

I need an algorithm that does this.  I know u can easily get the time in GMT+8:00 by just adding 8 hours to the GMT time. But what about the daylight saving time issue?

Since my web server is located in England, so the server time i get is in GMT.  What i am doing is creating a dicussion forum in php myself, and I want to display posts (or messages) in the user's local time which they can specify in their profiles.


Any help would be appreciated!!  (^O^)v
Avatar of us111
us111
Flag of Luxembourg image

If you have full access to your server (telnet), it's possible to replace /etc/localtime and your web server will display the correct time/date
Avatar of teuy
teuy

Are your user is coming from different timezone?
So you want to customize time appearance for each user?

I am not know much about daylight saving time because
the daylight saving time does not apply in my country. =)

see "When we change our clocks" page and
"Worldwide daylight saving" page in
http://webexhibits.org/daylightsaving/   for more details.

It may help you.
this is for date

<script>
function briefclientdate(){
var currentdate = new Date()
var numday = currentdate.getDate()
var nummonth = currentdate.getMonth()
var numyear = currentdate.getYear()
var monthname = "Jan."
// Start changing the month number to an actual labeled month name.
if (nummonth == 1){
var monthname = "Feb."
}
if (nummonth == 2){
var monthname = "March"
}
if (nummonth == 3){
var monthname = "April"
}
if (nummonth == 4){
var monthname = "May"
}
if (nummonth == 5){
var monthname = "June"
}
if (nummonth == 6){
var monthname = "July"
}
if (nummonth == 7){
var monthname = "Aug."
}
if (nummonth == 8){
var monthname = "Sept."
}
if (nummonth == 9){
var monthname = "Oct."
}
if (nummonth == 10){
var monthname = "Nov."
}
if (nummonth == 11){
var monthname = "Dec."
}
else{
//Do nothing.
}
document.write("" +monthname+ " " +numday+ ", " +numyear)
}

briefclientdate()
</script>


this is for time

<script>
function showClientTime(){
var startDate = new Date()
var currentHour = startDate.getHours()
var currentMin = startDate.getMinutes()
var timeSet = "AM"
if (currentHour > 12){
var timeSet = "PM"
currentHour = currentHour-12
}
if (currentHour == 0){
currentHour = 12
}
if (currentMin <= 9)
currentMin="0"+currentMin
else {
}
document.write(+currentHour+ ":" +currentMin+ " " +timeSet)
}

showClientTime()
</script>

Javascript is best suited for the thing u wanna do!

it will get the client time and display
just use these functions whereever u want to

HTH

JImmy
good idea, Jimmy.
Avatar of way_ching

ASKER

Thank you guys for giving me suggestions!  I'd appreciate it.

It's my bad that I should clarify what I am trying to do.
I have been writing a dicussion forum in php and want to allow time (of the posts or messages) to be displayed according to users' timezone.  This means that the time of the posts are already set when users posts the messages.  I want to customize time appearance for each user.

Jimmy282:  In my case, I don't think the Javascript can help me.  But Thanks anyways  :)

See http://www.timezoneconverter.com/ 
They have timezone converter to convert from time ( of one country) to time ( of another country ).
ASKER CERTIFIED SOLUTION
Avatar of teuy
teuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
us111:   I dont' see how the date function can help me.

teuy:  In your method, I'll probably have to create a table of possible countries in different time zones?


Actually, I know I can allow users to enter the offset in hours from the time in my server during registration.  But then I prefer to allow them to select the time zone they are in directly

Yes!

You may use 'ZoneID' to be primary key instead of 'Country' if you do not want to store many records in your database.

But I am not sure there are countries in the same timezone that has different DST start date or not.
teuy:   Thanks for your help!  I am able to solve my problem now!