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;
?>
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;
?>
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
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:
Use named time zones (Europe/Madrid
) rather than UTC offsets (+01:00
) since they take DST into account.
Set your app's time zone as default so you don't need to specify it every time:
date_default_timezone_set("Europe/Helsinki");
Never ever do date math yourself (e.g., don't add 86400 seconds manually to increase a day).
You must set the local time in php
function date_default_timezone_set
Example:
date_default_timezone_set("America/Fortaleza");
echo time(); // Local Time in America/Fortaleza
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
You can use date_default_timezone_set() function, For example
date_default_timezone_set('Asia/Dhaka');