How can I get the system time, like 10.23 PM, and how do I properly deal with time offsets and timezones?
+1
A:
Like this:
echo date('g.i A');
Check the PHP date() reference. date() takes an optional second argument which you can use to set a custom timestamp. Without this argument, date() returns the current time.
Tatu Ulmanen
2010-03-05 07:13:42
Thnak you very much am from india... so it displays 5.30 hrs back... any solution for this ????
Flins
2010-03-05 07:19:25
@Flins, use `date_default_timezone_set('Asia/Calcutta')` in the beginning of your script.
Tatu Ulmanen
2010-03-05 07:56:19
A:
if you use DB for storing data i propose you to use DB time and not windows system time. Why? Becouse DB time is always correct becouse DB is on production server but windows time depends on your clock settings of your computer, which can be wrong.
senzacionale
2010-03-05 07:22:36
The overhead of making a roundtrip to the DB just to get the time is worse than properly dealing with time offsets. If the clock of your server is wrong, fix it.
deceze
2010-03-05 07:24:51
if you have win application each customer can have different local time and if application depends on times this can be big problem. Time from DB is always the same even if your local time is 1.1.1901 so i do not agree with you deceze
senzacionale
2010-03-05 07:29:51
1) This question deals with PHP, not a local Win application. 2) If the DB is on the same computer as the application, the DB time will be wrong as well. 3) You do NOT make a roundtrip to the database to retrieve the time.
deceze
2010-03-05 07:34:07
1) this is true, but php can be use for win app as well 2) DB and app normaly are never in the same server! 3) here in our company we do exactly this, and we have more than 10000 customers!
senzacionale
2010-03-05 07:38:41
can any one please tell me how to get the ystem time. I have tried all codes but all gives me a difference of 5.30 hrs . am from india...
Flins
2010-03-05 07:41:57
1) If you program desktop apps in PHP you have bigger problems. ;) 2) You're making a roundtrip **over the network** just to get the time?! Even worse! 3) That doesn't make it any better. -- If you absolutely depend on synched time, do a proper time sync via NTP. In all other cases, just fix the local clock already. Even Windows automatically adjusts the clock over the internet now, how big of a problem can it be?
deceze
2010-03-05 07:45:05
1.) php, C#, java, c++2.) you do not get my point...Flinsdate_default_timezone_set('UTC');echo date("H:i);
senzacionale
2010-03-05 07:53:52
A:
<?php
$time_now=mktime(date('h'),date('i'),date('s'));
print date('h.i A',$time_now);
?>
muruga
2010-03-05 07:27:20