Solved

Inaccurate php time()

Posted on 2010-11-23
9
366 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 82

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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now