Link to home
Start Free TrialLog in
Avatar of jezella
jezellaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Inaccurate php time()

Hi all,

I'm stepping through my code in NuSphere where $time = time() results in the correct seconds (I think) since 1st Jan 1970 but $CurrentDate becomes inaccurate and often results in 2010-11-23 17:11 where the current GMT was NOT 17:11. What causes this and also where is the time being calculated from on my net connected PC with a local server. I can step through the code 10 minutes later and still it shows 17:11. My Windows clock is correct!! Code below. Thanks.

 <?php
$time=time();

$year=date("Y", $time);

$CurrentDate=date('Y-m-d H: m');
?>
Avatar of dsmile
dsmile
Flag of Viet Nam image

$CurrentDate return formatted date with current time(), so I think the result of date created from time() and date() without timestamp will be the same.

To compare the accuracy between this theory, just write some simple lines of code

$time=time();

echo $DateByTime=date('Y-m-d H: m', $time);

echo $CurrentDate=date('Y-m-d H: m');

These two results should be the same (as they'll be different by 1-2 microseconds only).

For the problem of unchanged time you have, try running your script from browser where it gets time directly from your PC (web server), not the virtual server of NuSphere.

Btw, if you want to get gmt time, then use gmdate() instead of date().
Avatar of Dave Baldwin
PHP normally returns Local time on the server,  not GMT.  If the server is in a different time zone from you, you should get a different time.  Repeating the "17:11" is probably a cache problem.  On this page http://us.php.net/manual/en/function.date-timezone-set.php is the function to set the time zone before you get the time on the server.
SOLUTION
Avatar of jezella
jezella
Flag of United Kingdom of Great Britain and Northern Ireland image

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
SOLUTION
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
ASKER CERTIFIED SOLUTION
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
Avatar of jezella

ASKER

See above
Avatar of jezella

ASKER

I had slight difficulty in awarding points here.  As always I appreciated input from members of EE and I know that often a comments will be added in a rush to receive points.  I do not think that this is wrong as it is possibly human nature and I have done it myself on occasions.

The comments added by dsmile though not correct caused me to look at my code much more closely as even what dsmile said did not solve the problem. I think for effort dsmile perhaps should receive some points.  

The link provided by Ray though not answering my question, probably will be a great help to me and others in the future.

I have selected a B grade where this is not a reflect of on any one person but does tell others seeking answers that the answer will not lie here apart from what I have provided.

Complete, accurate and easy to follow are marked as Yes for the reason, I provided the answer to my own question..
Please read this quote carefully (it was written carefully):

"The ISO DATETIME string can be gotten with date('c') -- it contains information to account for the difference to GMT (assuming you have used date_default_timezone_set() or equivalent to set the correct zone)."

In other words, crafting your own date() arguments is more difficult and prone to error, compared to using the built-in arguments.  Not as risky as REGEX, but still easy to get wrong.  Does "m" mean "month" or "minute" -- this is the sort of question that merits a click of the online man page.

All of the PHP functions are documented on the PHP.net web site.  So when you read the online documentation for these functions, as suggested by that quote and the article linked above, you will see that the question is well answered.

http://us.php.net/manual/en/function.date.php
http://us.php.net/manual/en/function.date-default-timezone-set.php

ID:34198651 was probably a useful comment, too.
Avatar of jezella

ASKER

I had slight difficulty in awarding points here.  As always I appreciated input from members of EE and I know that often a comments will be added in a rush to receive points.  I do not think that this is wrong as it is possibly human nature and I have done it myself on occasions.

The comments added by dsmile though not correct caused me to look at my code much more closely as even what dsmile said did not solve the problem. I think for effort dsmile perhaps should receive some points.  

The link provided by Ray though not answering my question, probably will be a great help to me and others in the future.

I have selected a B grade where this is not a reflect of on any one person but does tell others seeking answers that the answer will not lie here apart from what I have provided.

Complete, accurate and easy to follow are marked as Yes for the reason, I provided the answer to my own question..