Solved

Inaccurate php time()

Posted on 2010-11-23
9
374 Views
Last Modified: 2012-05-10
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
Comment
Question by:jezella
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:dsmile
ID: 34198584
$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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34198651
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
 

Assisted Solution

by:jezella
jezella earned 0 total points
ID: 34199607
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 100 total points
ID: 34202952
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 34206100
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
 

Author Comment

by:jezella
ID: 34246952
See above
0
 

Author Comment

by:jezella
ID: 34246964
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34247646
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
 

Author Closing Comment

by:jezella
ID: 34281010
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question