Adding time in PHP

2020-02-16 04:25发布

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)

8条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-16 04:39

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

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-02-16 04:45

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-16 04:48

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);
    
查看更多
Luminary・发光体
5楼-- · 2020-02-16 04:49

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);
查看更多
够拽才男人
6楼-- · 2020-02-16 04:49

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);
}
查看更多
啃猪蹄的小仙女
7楼-- · 2020-02-16 04:50

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;
查看更多
登录 后发表回答