-2

I have some php scripts on a hosting, but hosting time is different from my local time (GMT+8) How set the right time() script to be GMT+8 ?

When i use:

<?
echo time(); //it show me the hosting time;
?>
Mando Madalin
  • 193
  • 2
  • 3
  • 14
  • you can google it first! http://stackoverflow.com/questions/5454779/how-to-convert-php-date-formats-to-gmt-and-vice-versa – Developerium Nov 12 '13 at 12:20
  • 2
    Since `time()` returns a **timezone independent UNIX timestamp**, what result exactly do you expect and what do you get? – deceze Nov 12 '13 at 12:22
  • @deceze i want this, i m workin with time() function to make timestamps for some new record i have to add on table. time() is showing me the hosting server time. but i am on GMT+8 region, so whenever i use time() i want to give me the GMT+8 time – Mando Madalin Nov 12 '13 at 12:31
  • Again, please prove to us how exactly the output of `time()` differs from what you *expect* in concrete terms. Your question is vague and the onus is on you to prove that something that works for everyone else doesn't work for you. My `time()` here currently gives me `1384260111`. What else do you expect and why? – deceze Nov 12 '13 at 12:41
  • @deceze i want a function to give me the time in seconds (as time() funciton) for Asia/Hongkong time zone. is there anyway to get time zone of hongkon in seconds ? – Mando Madalin Nov 12 '13 at 12:47
  • 2
    *facepalm* Tell us a concrete example of a ***value*** you expect and what value you're getting instead! UNIX timestamps are **independent** of timezones, so "time in seconds for Hong Kong" makes no sense! – deceze Nov 12 '13 at 12:51
  • @deceze what else concrete example do you want to tell me more then what i want? time() is echo the time since epoch, i want a function to echo in seconds the hongkon time zone. time() is echo me 1384259580 (12-11-2013 05:33) function_that_i_need() is to echo me *seconds* (12-11-2013 20:33) hongkong time – Mando Madalin Nov 12 '13 at 12:56
  • So what would that value look like? There's no standard anywhere that defines "seconds Hong Kong time". There's the universal UNIX epoch standard, there's no "Hong Kong epoch" standard. So if you want something like that, you either need to clarify how exactly that should look like or, more likely, you misunderstand what the UNIX epoch is and how to use it. – deceze Nov 12 '13 at 13:01
  • @deceze look, i have a script that enter new records into db. i used the time() to give me the insert date as seconds coz is more easy for me to use after the strtotime function to search intro records from specific date. is there any way i could save the hongkong time as seconds format, and after to use the date('d-m-Y h:s',$storedseconds) to give me the time for hongkong? – Mando Madalin Nov 12 '13 at 13:05
  • So you're saying the date is displayed incorrectly **after running the epoch seconds through `date()`**?! That's an entirely different problem then. – deceze Nov 12 '13 at 13:10
  • 1
    @MandoMadalin: All you need to do is set timezone, with `date_default_timezone_set()`, and calling `date()` function will print correct offset. – Glavić Nov 12 '13 at 13:11
  • Read this: http://stackoverflow.com/a/4812178/476 – deceze Nov 12 '13 at 13:12
  • Thank you, i figure it out! very hard to understand each others but finally i did it `date_default_timezone_set('Asia/Hong_Kong'); $time = strtotime(date('c', time())); echo date('d-m-Y H:m:s',$time);` – Mando Madalin Nov 12 '13 at 13:17
  • 2
    @MandoMadalin: you already had 3 1hour old answers which are saying to use `date_default_timezone_set()`. Strange you didn't try them.... – Glavić Nov 12 '13 at 13:26

5 Answers5

4

time() will always return the number of seconds since the epoch. The code below will print the same twice.

date_default_timezone_set('Europe/London');
echo time();

date_default_timezone_set('America/Cuiaba');
echo time();

The concept of Unix Timestamp does not carry time zone information by design. A given timestamp is always the same regardless of time zone. (The number of seconds since 1970-01-01 00:00:00 UTC) When you want to express a timestamp with time zone taken into account, you will adjust the resulting date with the current time zone's offset.

So when using the 'c' format option to PHP's date (which does reflect time zone information) you will see different representation of the same timestamp

date_default_timezone_set('Europe/London');
echo time();
echo date('c')

date_default_timezone_set('America/Cuiaba');
echo time();
echo date('c');

Will output:

1384259474
2013-11-12T12:31:14+00:00
1384259474
2013-11-12T09:31:14-03:00
marekful
  • 14,986
  • 6
  • 37
  • 59
  • Good, now please help me how to make whenver i trigger the time() function to show me the Asia/Hongkong time in seconds? now matter where scripts are hosted – Mando Madalin Nov 12 '13 at 12:42
  • 1
    You did not understand what I said. `time` will NOT take time zone into account. You have to use the `date` function. You do not want `time` to behave differently. You can make any calculations you need and then _display_ the result with time zone information. – marekful Nov 12 '13 at 12:43
  • Maybe if you could detail what you're exactly trying to achieve, I could give you a more verbose explanation why you don't need `time()` to take time zone into account – marekful Nov 12 '13 at 12:45
  • i want a function to give me the time in seconds (as time() funciton) for Asia/Hongkong time zone. is there anyway to get time zone of hongkon in seconds ? – Mando Madalin Nov 12 '13 at 12:49
  • You can do that, but what I'm saying is that most likely you don't need to. Would you like to share the exact problem you are trying to solve? – marekful Nov 12 '13 at 12:53
  • I have an database where i save some records, whenever i insert new record, i use the time() function to store the saved record date. time() is echo the time since epoch, i want a function to echo in seconds the hongkon time zone. time() is echo me 1384259580 (12-11-2013 05:33) function_that_i_need() is to echo me seconds (12-11-2013 20:33) hongkong time – Mando Madalin Nov 12 '13 at 13:00
  • In this case, what _may_ be important to you is the database server's time zone setting but not necessarily. Try to think of it this way: assume the database server is in UTC. When the record is created in database you just use PHP's `time()` or even MySQL's `NOW()` function. This will save in UTC. Now, when you read the creation date of a record back, you have it in UTC, but you may display it in _any_ time zone you just need. – marekful Nov 12 '13 at 13:03
  • Try to answer this question to yourself: Why is it important to store creation time in GMT+8? (It is not. Like I said above, `timestamp` does not care about time zones). Time zones play a role when representing a timestamp in human readable format. – marekful Nov 12 '13 at 13:06
  • Good to hear that, but i don't know how to display it in what time i want. i use this for now echo date('d-m-Y h:s',$mystoredseconds); how i can make it to show hongkong time? thank you – Mando Madalin Nov 12 '13 at 13:08
  • 1
    So if you want display your stored seconds (stored in UTC), you can do: `date('c', $mystoredseconds)`. This will display in the system's time zone. Use `date_default_timezone_set()` in your PHP code to change PHP's time zone, then when you repeat `date('c', $mystoredseconds)` it will print the date in a different time zone. – marekful Nov 12 '13 at 13:11
  • Thank you, i figure it out! very hard to understand each others but finally i did it `date_default_timezone_set('Asia/Hong_Kong'); $time = strtotime(date('c', time())); echo date('d-m-Y H:m:s',$time);` – Mando Madalin Nov 12 '13 at 13:15
1

Your assumption is not correct:

int time ( void )

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

The Unix Epoch is a fixed moment in time. If you really get an invalid timestamp, your hosting provider has not cared to set the server's clock.

If you want to do decent time zone aware date handling I suggest you learn about the DateTime class and friends and:

  1. Use named time zones (Europe/Madrid) rather than UTC offsets (+01:00) since they take DST into account.

  2. Set your app's time zone as default so you don't need to specify it every time:

    date_default_timezone_set("Europe/Helsinki"); 
    
  3. Never ever do date math yourself (e.g., don't add 86400 seconds manually to increase a day).

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • I highly doubt this has anything to do with the server's clock not being set. The OP clearly stated he is only trying to display the time in GMT+8 (which differs from the time is hosting provider is using) – Charlie74 Nov 12 '13 at 12:28
  • 1
    @Charlie74 - He doesn't really say what he wants to do. He says that `echo time()` output's is wrong, which is obviously hard to believe. – Álvaro González Nov 12 '13 at 12:30
  • @Charlie74 time output is fine, but i want a function to give me the time in seconds (as time() funciton) for Asia/Hongkong time zone. is there anyway to get time zone of hongkon in seconds ? – Mando Madalin Nov 12 '13 at 12:49
  • 1
    @MandoMadalin - At this point, considering all the answers you've got so far, I wonder if this is actually a question about relativity and you want to take into account the amount of nano-seconds that your server and the city of Hong-Kong have shifted away since 1970 due to Earth's different rotation. I regret to inform that PHP timestamps handling uses integers, thus has a resolution of 1 second. – Álvaro González Nov 12 '13 at 15:18
0

You must set the local time in php

function date_default_timezone_set

List of Supported Timezones

Example:

date_default_timezone_set("America/Fortaleza"); 
echo time(); // Local Time in America/Fortaleza
juniorb2s
  • 242
  • 4
  • 11
0

Add 28800 (8 hrs converted to seconds) to the output of time()

<?php echo (time()+28800); ?>

I am not sure but, time() returns timestamp which is equivalent to GMT thus, adding timestamp of 8 hours will give you GMT+8. Similarly, you can even subtract the time also

Adi
  • 407
  • 1
  • 4
  • 13
-3

You can use date_default_timezone_set() function, For example

date_default_timezone_set('Asia/Dhaka');