Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

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');
?>
0
jezella
Asked:
jezella
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
dsmileCommented:
$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().
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
jezellaAuthor Commented:
Hi dsmile and DaveBaldwin.

I have discovered my mistake.  For my minute in the time I had written m where I ought to have written i.  This resulted in displaying the month where I took this a 11 minutes past the hour where in fact it was indicating November.

I appreciate the help but I suspect that the code of dsmile would only result in displaying the month and not the minute. I have tried this and this does appear to be the case.

The idea of dsmile to use gmdate is a good one and does work on a US based server and shows the time correct to my UK audience.

I shall leave my question open here for a short time as the comments I have made over the code supplied by dsmile may need correcting as I could be wrong.

Jezella  
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
dsmileCommented:
You're right, jezella, that using 'm' to represent 'second' is wrong ;)
I was just too eager to give solution without checking for syntax in your code there :)

Happy coding then.
0
 
Ray PaseurCommented:
Here is an article that may be helpful to you.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

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).
0
 
jezellaAuthor Commented:
See above
0
 
jezellaAuthor Commented:
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..
0
 
Ray PaseurCommented:
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.
0
 
jezellaAuthor Commented:
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..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now