我正在从mysql数据库中提取一个日期时间,我想将它加上X个小时,然后将其与当前时间进行比较。到目前为止,我得到了
$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);然后我尝试了$dbTime + strtotime("4小时“);但是4小时似乎使当前时间增加了4小时,而不是原始的4小时。如何将X小时添加到dbTime?
注意:我使用的是php 5.1.2,所以date_add不能工作(5.3.0)
发布于 2009-09-21 21:23:11
time()和strtotime()生成unix时间戳(以秒为单位),因此您可以执行类似以下操作,前提是您的db和比较:
$fourHours = 60 * 60 * 4;
$futureTime = time() + $fourHours;发布于 2009-09-21 21:37:56
然后我尝试了$dbTime +
(“4小时”);但是4小时似乎使当前时间增加了4小时,而不是原始的4小时。如何将X小时添加到dbTime?
strtotime有一个可选的第二个参数。在那里提供一个Unix时间戳,输出将相对于该日期而不是当前日期。
$newTime = strtotime('+4 hours', $dbTime);您还可以利用Unix时间戳是以秒为单位的这一事实-如果您知道四个小时是以秒为单位的,则可以将其添加到时间整数值中。
发布于 2009-09-21 21:44:42
这里有相当多的选项:
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);https://stackoverflow.com/questions/1456886
复制相似问题