Solved

Server time, not local user's time  (php, mySQL)

Posted on 2012-03-24
7
431 Views
Last Modified: 2012-03-25
A quick question:

Timezones around the world... users in different timezones.

I need to compare when a record was updated, not based on local time, but on server time.

I have a datetime field in mySql, default is: 0000-00-00 00:00:00       

I have a form, and that form processing should update a field in a record. If someone in London and someone in Chicago update their record nearly simultaneously, the database should show the times as whatever was correct at the server at the time, not local time. Even though Peter and Paul are 6 hours apart in time zones, if they update nearly simultaneously, I would want:

 2012-03-24 23:49:03       Peter, London
 2012-03-24 23:49:00       Paul, Chicago

Will the php function Now() update the record with server time, or local time? If local, then what function do I need to use, instead.

Thanks!

Dennis
0
Comment
Question by:dtleahy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 375 total points
ID: 37762189
That's a MySQL function, not a PHP function, and it uses the Server time.  http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_now
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 375 total points
ID: 37762191
And actually, PHP Date and time functions will use the server time unless you have changed the timezone.  Without you telling it, it doesn't even know the users time zone.
0
 

Author Comment

by:dtleahy
ID: 37762217
Thanks Dave, for the quick reply,

So...
(from the php file)
$sql="UPDATE mytable SET zdatetime=Now() WHERE (zuserid = $row[vuserid ])";	

Open in new window


Simple as this?

Do I need to format it at all? I don't need to display the data in the zdatetime field, I just need to make a comparison, so I can (for example) query to get the last 10 people who have activated.

Dennis
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 375 total points
ID: 37762224
That's what I would do except it's NOW(), not Now().  Then you can do:

$qlast10 = "SELECT * FROM mytable ORDER BY zdatetime DESC LIMIT 10";

to get the latest 10 entries.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 37762443
This article will give you some background on the use of timezones in PHP and MySQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Executive summary: PHP tries to be timezone sensitive.  See the output of date('c') to understand its use of UTC offset.  But MySQL does not carry a timezone offset in the data type DATETIME.  So one strategy is to set your MySQL clock to UTC and use date_default_timezone_set() in PHP.  That will keep your MySQL data base consistent and you can still have the option to show local times to clients.
0
 

Author Comment

by:dtleahy
ID: 37762738
Thanks, Ray!

Excellent info at that link! Thank you so much for taking the time (a few years ago) to amass that accumulated knowledge into what is a whitepaper. Great resource!

In my current programming situation, the only thing this datetime field will be used for is to do an ORDER BY in the SQL statement. I'll never display the field data and the people around the world will never need to know the offset from GMT, or do any datetime math for example.

This has nothing to do with auctions, but as a good example, imagine an item being auctioned off and people around the world are bidding. I would need to know what time they hit the server, not what time it was at their particular locality.

The exact reason I want this is so that I can display a list of the most recent 10 people that have signed-up to a website I'm building, (no matter where they are in the world.)

Dennis
0
 

Author Closing Comment

by:dtleahy
ID: 37764061
Thanks, Dave and Ray! You guys are gems!<br />-Dennis
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 count occurrences of each item in an array.

724 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