【问题标题】:strototime() relative dates to the timestrototime() 相对日期时间
【发布时间】:2015-11-27 14:02:24
【问题描述】:

目前,我将时区设置为America/New York

我的时间戳为 1448933400,即 2015 年 11 月 30 日星期一 - 晚上 8:30。我想从当前时间戳中得到一个相对时间作为“下个月的第一个太阳”。

所以我正在执行以下代码:

strtotime("First Sun of Next Month", 1448933400);

我遇到的问题是,1449378000 的返回值。

这不是晚上 8:30,而是凌晨 12:00。有没有一种简单的方法来获取相对日期以保留源时间戳的相对时间?

【问题讨论】:

  • 使用 DateTime 和 DateInterval 对象

标签: php strtotime


【解决方案1】:

有很多可能的答案,但您可以做的是首先计算时间戳当天的时间偏移并将其应用于结果:

<?php
$stamp = 1448933400;
$offset = $stamp - strtotime(date("Y-m-d", $stamp));
var_dump(strtotime("First Sun of Next Month", $stamp) + $offset);
?>

输出

int(1449451800) // or "2015-12-06 20:30:00"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多