Solved

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

Posted on 2012-03-24
7
427 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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 109

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complex SQL statement in VB.NET 7 31
Wifi connection to laptop without router windows 10 11 37
Redirect 301 from one address  to another 5 25
Link failure 16 32
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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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 …

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