在 PHP 中添加时间

发布于 2024-08-05 04:33:24 字数 309 浏览 0 评论 0原文

我正在从 mysql 数据库中提取日期时间,我想向其中添加 X 小时,然后将其与当前时间进行比较。到目前为止,

$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);

我尝试了 $dbTime + strtotime("4 小时");但 4 小时似乎是在当前时间上添加了 4 小时,而不是原始的 4 小时。如何向 dbTime 添加 X 小时?

注意:我使用的是 php 5.1.2,所以 date_add 不起作用(5.3.0)

I am pulling a datetime from a mysql db and i would like to add X hours to it then compare it to the current time. So far i got

$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);

then i tried $dbTime + strtotime("4 hours"); but 4 hours seem to add 4hrs to the current time instead of raw 4hours. How do i add X hours to dbTime?

NOTE: I am using php 5.1.2 so date_add doesnt work (5.3.0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

凡间太子 2024-08-12 04:33:24

有很多

1.2.3.4.5

$result = mysql_query("SELECT myDate FROM table");
$myDate = mysql_result($result, 0);
$fourHoursAhead = strtotime("+4 hours", strtotime($myDate));

// same first two lines from above
$fourHoursAhead = strtotime($myDate) + 4 * 60 * 60;

选择

$result = mysql_query("SELECT UNIX_TIMESTAMP(myDate) FROM table");
$myDate = mysql_result($result, 0);
$fourHoursAhead = $myDate + 4 * 60 * 60;

$fourHoursAhead = strtotime("+4 hours", $myDate);

这里

$result = mysql_query("SELECT UNIX_TIMESTAMP(DATE_ADD(myDate, INTERVAL 4 HOUR))");
$fourHoursAhead = mysql_result($result, 0);

You have quite a few options here:

1.

$result = mysql_query("SELECT myDate FROM table");
$myDate = mysql_result($result, 0);
$fourHoursAhead = strtotime("+4 hours", strtotime($myDate));

2.

// same first two lines from above
$fourHoursAhead = strtotime($myDate) + 4 * 60 * 60;

3.

$result = mysql_query("SELECT UNIX_TIMESTAMP(myDate) FROM table");
$myDate = mysql_result($result, 0);
$fourHoursAhead = $myDate + 4 * 60 * 60;

4.

$fourHoursAhead = strtotime("+4 hours", $myDate);

5.

$result = mysql_query("SELECT UNIX_TIMESTAMP(DATE_ADD(myDate, INTERVAL 4 HOUR))");
$fourHoursAhead = mysql_result($result, 0);
若无相欠,怎会相见 2024-08-12 04:33:24

然后我尝试了 $dbTime + strtotime("4 小时");但 4 小时似乎是在当前时间上添加了 4 小时,而不是原始的 4 小时。如何向 dbTime 添加 X 小时?

strtotime 有一个可选的第二个参数。在那里提供 Unix 时间戳,输出将相对于该日期而不是当前日期。

$newTime = strtotime('+4 hours', $dbTime);

您还可以利用 Unix 时间戳是基于秒的这一事实 - 如果您知道四小时以秒为单位,则可以将其添加到时间整数值中。

then i tried $dbTime + strtotime("4 hours"); but 4 hours seem to add 4hrs to the current time instead of raw 4hours. How do i add X hours to dbTime?

strtotime has an optional second argument. Provide a Unix timestamp there and the output will be relative to that date instead of the current date.

$newTime = strtotime('+4 hours', $dbTime);

You can also use the fact that Unix timestamps are seconds-based - if you know what four hours are in seconds, you can just add that to the time integer value.

反差帅 2024-08-12 04:33:24

time() 和 strtotime() 会产生以秒为单位的unix时间戳,因此您可以执行如下操作,前提是您的数据库并进行比较:

$fourHours = 60 * 60 * 4;
$futureTime = time() + $fourHours;

time() and strtotime() result in unix timestamps in seconds, so you can do something like the following, provided your db and do your comparison:

$fourHours = 60 * 60 * 4;
$futureTime = time() + $fourHours;
时光倒影 2024-08-12 04:33:24

strtotime("+4 小时", $dbTime);

第二个参数是时间戳,用作计算相对日期的基础;它默认为当前时间。查看文档

编辑:
对于较短的时间(最多 1 周),在时间戳中添加秒数是完全可以接受的。一周总有(7 * 24 * 3600)秒;一个月或一年都不能说同样的话。此外,unix 时间戳只是自 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)以来经过的秒数。这不受时区或夏令时的影响。仅当将 UNIX 时间戳转换为实际日历日期和时间时,时区和夏令时才重要。

strtotime("+4 hours", $dbTime);

The second argument is the timestamp which is used as a base for the calculation of relative dates; it defaults to the current time. Check out the documentation.

Edit:
For short periods of time, max 1 week, adding seconds to a timestamp is perfectly acceptable. There is always (7 * 24 * 3600) seconds in a week; the same cannot be said for a month or year. Furthermore, a unix timestamp is just the number of seconds that have elapsed since the Unix Epoch (January 1 1970 00:00:00 GMT). That is not effected by timezones or daylight-savings. Timezones and daylight-savings are only important when converting a unix timestamp to an actual calendar day and time.

岁月打碎记忆 2024-08-12 04:33:24

我倾向于使用 time() 函数,手册中的此页面显示它们显示未来一周的日期:
https://www.php.net/manual/en/function.time。 php

I tend to use the time() function, and this page from the manual shows them displaying the date a week in the future:
https://www.php.net/manual/en/function.time.php

毁虫ゝ 2024-08-12 04:33:24

我的做法如下:

  1. 使用 UNIX_TIMESTAMP() 函数。

  2. UNIX 时间戳以秒为单位,因此添加 4*60*60

  3. 使用 PHP 的 localtime() 将修改后的 UNIX 时间戳转换为日期 或 strftime() 函数。

    query("SELECT UNIX_TIMESTAMP(someDatetimeColumn) ...");
    。 。 。
    $dbTimeAdjusted = 本地时间($row[0] + 4*60*60);
    

Here's how I'd do it:

  1. Pull the time from the database using the UNIX_TIMESTAMP() function.

  2. The UNIX timestamp is in seconds, so add 4*60*60 to it.

  3. Convert the modified UNIX timestamp to a date using PHP's localtime() or strftime() function.

    query("SELECT UNIX_TIMESTAMP(someDatetimeColumn) ...");
    . . .
    $dbTimeAdjusted = localtime($row[0] + 4*60*60);
    
黒涩兲箜 2024-08-12 04:33:24

也许最安全的比较方法就是在 SQL 中进行比较

SELECT * FROM my_table WHERE someDateTimeColumn < DATE_ADD(NOW(), INTERVAL 4 hour)

,并且由于您在 PHP 中组装它,因此您可以用代码需要比较的任何内容动态替换“4 小时”位。

(注意:将整个计算放在与列比较的另一侧,允许 MySQL 每个查询执行一次计算,而不是每行一次,并且还可以使用表的索引(如果该列有索引)。)

Probably the safest way to do the compare is right in the SQL

SELECT * FROM my_table WHERE someDateTimeColumn < DATE_ADD(NOW(), INTERVAL 4 hour)

And since you're assembling it in PHP, you can dynamically replace the "4 hour" bit with whatever your code needs to compare.

(Note: putting the entire calculation on the other side of the comparison to the column allows MySQL to do the calculation once per query, rather than once per row, and also use the table's index, if that column has one.)

话少情深 2024-08-12 04:33:24

假设数据库返回的时间戳是 SQL 格式,则以下内容应该可以正常工作:

$dbTime = strtotime($row[0]);
$nowTime = time();

$future_dbTime = strtotime("+4 hours", $dbTime);

$diff_time_seconds = $nowTime - $dbTime;

if ($diff_time_seconds > 0) {
       echo "The current time is greater than the database time by:\n";
       $not_equal = true;

    }
if ($diff_time_seconds == 0) {
       echo "The current time is equal to the database time!";
    }
if ($diff_time_seconds < 0) {
       echo "The current time is less than the database time by:\n";
       $not_equal = true;
    }

if ($not_equal) {
$diff_time_abs_seconds = abs($diff_time_seconds);
echo date('h:m:s', $diff_time_abs_seconds);
}

Assuming that the timestamp returned by the DB is in SQL format, the following should work fine:

$dbTime = strtotime($row[0]);
$nowTime = time();

$future_dbTime = strtotime("+4 hours", $dbTime);

$diff_time_seconds = $nowTime - $dbTime;

if ($diff_time_seconds > 0) {
       echo "The current time is greater than the database time by:\n";
       $not_equal = true;

    }
if ($diff_time_seconds == 0) {
       echo "The current time is equal to the database time!";
    }
if ($diff_time_seconds < 0) {
       echo "The current time is less than the database time by:\n";
       $not_equal = true;
    }

if ($not_equal) {
$diff_time_abs_seconds = abs($diff_time_seconds);
echo date('h:m:s', $diff_time_abs_seconds);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文