Solved

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

Posted on 2012-03-24
7
424 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
  • 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 108

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 33
test if query has no results 2 19
How many transactions can mysql handle? 3 22
Method for submitting dynamically created rows 20 17
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

920 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

15 Experts available now in Live!

Get 1:1 Help Now