php date_default_timezone_set time()VS mysql now()

时间:2022-10-28 22:46:36

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();

date_default_timezone_set( “欧洲/伦敦”); $ time = time();

or

要么

using mysql function now() ?

使用mysql函数now()?

Sometime customer can select a time for delivery or collection.

有时客户可以选择交货或收货的时间。

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

注意:在英国,我们每年更改两次时间!

3 个解决方案

#1


5  

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.

如果将时间发送到数据库,请使用NOW();更少的开销,并且服务器时区有望总是正确且比PHP的时区更不可变。如果我们只是在谈论显示,而没有实际进行数据库工作,那么仅仅为了获得时间而运行mysql查询就太过分了。

#2


2  

In PHP I usually do the following:

在PHP中,我通常会执行以下操作:

date_default_timezone_set('GMT');

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

然后,在连接到MySQL服务器时,我执行以下查询:

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

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

这可以确保PHP和MySQL都使用相同的时区,因此NOW()和日期('Y-m-d H:i:s')都应该产生相同的结果。

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

关于日光变化,它们应该不是问题(如果您保持软件更新)。

#3


1  

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

在这个问题中,一个重要的考虑因素是PHP和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.

考虑使用MySQL NOW()函数将日期写入数据库的情况,并单独地查询要求在过去24小时内完成的所有条目,使用PHP日期函数在查询中构建时间。如果PHP服务器上的时间与SQL服务器不同步,则可能会导致您错过报告中的记录(或者连续几天将它们加倍,具体取决于它们的不同步方式) 。这可能会导致错过交货等。

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.

这样做的结果是你应该小心使用日期(特别是日期时间)。您是否使用PHP或MySQL日期功能并不重要,但您应该尝试使用相同的平台进行查询,就像您以前更新一样。

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.

当然,如果两个系统位于同一台服务器上,这不是问题,但您不应该假设情况总是如此。

#1


5  

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.

如果将时间发送到数据库,请使用NOW();更少的开销,并且服务器时区有望总是正确且比PHP的时区更不可变。如果我们只是在谈论显示,而没有实际进行数据库工作,那么仅仅为了获得时间而运行mysql查询就太过分了。

#2


2  

In PHP I usually do the following:

在PHP中,我通常会执行以下操作:

date_default_timezone_set('GMT');

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

然后,在连接到MySQL服务器时,我执行以下查询:

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

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

这可以确保PHP和MySQL都使用相同的时区,因此NOW()和日期('Y-m-d H:i:s')都应该产生相同的结果。

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

关于日光变化,它们应该不是问题(如果您保持软件更新)。

#3


1  

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

在这个问题中,一个重要的考虑因素是PHP和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.

考虑使用MySQL NOW()函数将日期写入数据库的情况,并单独地查询要求在过去24小时内完成的所有条目,使用PHP日期函数在查询中构建时间。如果PHP服务器上的时间与SQL服务器不同步,则可能会导致您错过报告中的记录(或者连续几天将它们加倍,具体取决于它们的不同步方式) 。这可能会导致错过交货等。

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.

这样做的结果是你应该小心使用日期(特别是日期时间)。您是否使用PHP或MySQL日期功能并不重要,但您应该尝试使用相同的平台进行查询,就像您以前更新一样。

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.

当然,如果两个系统位于同一台服务器上,这不是问题,但您不应该假设情况总是如此。