简体   繁体   中英

php date_default_timezone_set time() VS mysql now()

I am developing online shopping system. Only UK customers can place an order from the website.

I am wondering which is best method for me?

date_default_timezone_set("Europe/London"); $time = time();

or

using mysql function now()?

Sometime customer can select a time for delivery or collection.

Note: In the UK, we change the time twice a year!

If the time is being sent to the database, use NOW(); less overhead, and the server time zone is hopefully always going to be correct and less mutable than PHP's time zone. If we're just talking about display, without actually doing database work, then it's excessive to run a mysql query solely to get the time.

In PHP I usually do the following:

date_default_timezone_set('GMT');

And then, upon connecting to the MySQL server, I execute the following query:

'SET time_zone = "' . date_default_timezone_get() . '";'

This makes sure that both PHP and MySQL are using the same timezone, so NOW() and date('Ymd H:i:s') should both yield the same result.

Regarding the daylight changes, they shouldn't be a problem (if you keep your software updated).

One massive consideration in this question is whether the times are the same between PHP and MySQL?

If the two are running on the same machine then the answer is likely to be 'yes', but if they're on separate machines then there's a strong possibility that they may in fact be different.

Consider a scenario where dates are written to a database using the MySQL NOW() function, and separately you have a query asking for all entries made in the last 24 hours, building the time in the query using the PHP date functions. If the time on the PHP server is out of sync with the SQL server, it opens up the possibility that you may miss records from your report (or get them doubled-up on consecutive days, depending on which way out of sync they are). This could lead to deliveries being missed, etc.

The upshot of this is that you should be careful to be consistent with your use of dates (and in particular datetimes). It doesn't really matter whether you use the PHP or MySQL date functionality, but you should try to use the same platform to query as you used to update.

Of course, it's not always going to be critical in this way, and you can be pragmatic about it - sometimes it's just too inconvenient to go back to the DB, just to find out what the time is, But when it's important. you should be careful.

Naturally, if the two systems are on the same server, this is a non-issue, but you shouldn't assume this will always be the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM