【问题标题】:`Carbon->timestamp` but without seconds`Carbon->timestamp` 但没有秒
【发布时间】:2019-12-08 17:03:39
【问题描述】:

有任何Carbon方式来获取Carbon对象的时间戳,但没有秒?

我需要比较两个Carbon 对象,但我不想考虑秒...

if ($carbon1->timestamp > $carbon2->timestamp) {
   // Do something...
}

我不喜欢使用像

这样的解决方案
$timestampWithoutSeconds = substr($carbon1->timestamp, 0, 5);

【问题讨论】:

    标签: php timestamp php-carbon


    【解决方案1】:

    假设您的时间戳以毫秒为单位,您可以从中减去秒数。

    假设您的时间戳是 1564562500699,即 2019 年 7 月 31 日星期三 10:41:40 GMT+0200(中欧夏令时间)

    $timestamp = 1564562500699;
    $milliseconds = $timestamp % (60*1000); // 40699
    $timestampWithoutSeconds = $timestamp - $milliseconds;
    
    // Your condition
    

    如果您想要一个看起来像 subSecondssecond 一起使用的 Carbon 解决方案,getter 会起作用:

    $carbon1->subSeconds($carbon1->second);
    

    【讨论】:

    • 刚刚使用 Carbon 的解决方案进行了编辑。 @tomloprod
    【解决方案2】:

    您可以为碳对象铺地板:

    if ($carbon1->floorMinute() > $carbon2->floorMinute()) {
       // Do something...
    }
    

    ->roundMinute() 取决于您想要更低或最接近的分钟。

    【讨论】:

    • 有趣!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2017-04-08
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2019-10-31
    相关资源
    最近更新 更多