什么是从Timestamp php中减去6个月的最简单方法

问题描述 投票:0回答:2

我有一个时间戳格式的日期。我需要从中减去6个月。这是我目前这样做的方式

 $begin = Carbon::createFromTimestamp($begin, Timezone::IST)->subMonths(6);

但我再次需要时间戳格式,因为我将其保存为整数(laravel)。有没有更好的方法来做到这一点

php laravel timestamp epoch
2个回答
1
投票

像这样

 $Datetime = new DateTime($time);
 $Datetime->modify('-6 months');
 echo $Datetime->format('Y-m-d');

http://php.net/manual/en/class.datetime.php

UPDATE

这是什么?

但我再次需要时间戳格式,因为我将其保存为整数(laravel)。有没有更好的方法来做到这一点

我的意思是int看起来像什么。你可以使用像这样的DateTime::createfromformat( 'Y-m-d', '2018-01-01')然后输出它作为$Datetime->gettimestamp();的时间戳

所以完整的代码将接近:

$Datetime- = DateTime::createfromformat( '{your date format}', $datestring);
$Datetime->modify('-6 months');
$timestamp = $Datetime->gettimestamp();

你不应该减去任意数秒,因为这不会占闰年,或奇数天数。日期时间相对格式是专门为此提供的,因此最好使用它们。

UPDATE2(适用于碳)

如果你看一下Carbon的来源,它是DateTime的扩展,因此可能提供相同的功能,还有更多。所以,即使这可能工作Carbon::createFromTimestamp($begin, Timezone::IST)->subMonths(6)->gettimestamp()

正如碳源的这个tid位所示

public function diffInSeconds(Carbon $dt = null, $abs = true)
{
    $dt = $dt ?: static::now($this->getTimezone());
    $value = $dt->getTimestamp() - $this->getTimestamp();
    return $abs ? abs($value) : $value;
}

特别是$this->getTimestamp(),这并不是一个大惊喜,因为它扩展了DateTime。也就是说,我会避免使用timestamp的公共财产,因为它违反了黑匣子协议(这就是我所说的(BBP)),其中一个类应该是一个黑盒子,它的功能。而且你应该只使用它的公共接口(哪些接口不包含属性)从类访问数据。这只是我的意见。

现在,如果你真的深入研究碳的源代码,那么这个代码Carbon::createFromTimestamp($begin, Timezone::IST)->subMonths(6)可能做的事非常像:

$this->datetime = new Datetime($begin, Timezone::IST);
$this->datetime->modify('-6 months');

特别

class Carbon extends DateTime{

     //-------- create---------

     public static function createFromTimestamp($timestamp, $tz = null)
    {
        return static::now($tz)->setTimestamp($timestamp);
    }

    public static function now($tz = null)
    {
        return new static(null, $tz);
    }
     /*
        the code "new static" can be a bit confusing, but this is just           
        one of many ways to call "new Carbon()", which in turn is calling
        new "DateTime()". They use static so if you extended Carbon it will
        work (as opposed to using "new self") a.k.a "new static" uses late   
        static binding.  And they also use static, because it's cleaner then
        doing something like "new __CLASS__", which I believe will not let
        you pass in arguments (without assigning the class name to a 
        variable). Where as new static($arg) works fine.  In any case it's
        generally preferred to use something like "new static" then to
        reference the class name directly as in "new Carbon".  Obviously
        this is so if the class name changes there are no repercussions on
        the code, no code to update.
    */

    //-------- subMonth ---------
    public function subMonth($value = 1)
    {
        return $this->subMonths($value);
    }

    public function subMonths($value)
    {
        return $this->addMonths(-1 * $value);
    }

    public function addMonths($value)
   {
        if (static::shouldOverflowMonths()) {
            return $this->addMonthsWithOverflow($value);
        }
        return $this->addMonthsNoOverflow($value);
    }

    public function addMonthsWithOverflow($value)
    {
        return $this->modify((int) $value.' month');
    }

    public function addMonthWithOverflow($value = 1)
    {
        return $this->addMonthsWithOverflow($value);
    }

}

所以它调用4或6个额外的函数调用,但最后它基本相同。从github source code采取。

为了创建它们只是用当前日制作一个DateTime对象(子),然后用时间戳设置它的日期。

所有他们正在做的是拿6你给他们*-1所以它是-6然后做$this->modify('-6 months'),因为它延伸DateTime大致相当。

基本上我上面粘贴的所有代码都可以用3行完成。但是,如果是我,我仍然会使用Carbon,因为它是“框架”的方式,所以它更适合一点。函数调用也是吱吱声,性能方面你不会真正注意到几纳秒的差异。因此,我一如既往地倾向于可读性(在框架内)而不是性能。

作为一方,我从未使用过Carbon,但它看起来很有趣。我钻了更深的然后我可能应该有,但我一直在考虑做一些非常相似的东西,所以也许我只会“作曲家”中的碳...大声笑......年纪越长,我越来越懒,6年以前,我会在钥匙上敲一下,告诉我有一些非常相似的东西。但作曲家不在那儿,所以......


0
投票

你可以使用->timestamp

$begin = Carbon::createFromTimestamp($begin, Timezone::IST)->subMonths(6)->timestamp;

但是,如果您将此日期和所有其他日期保存为DB作为时间戳,则可以将$dateFormat属性设置为U

protected $dateFormat = 'U';

然后,$begin将由Laravel自动转换为Carbon实例。所以,你可以这样做:

$begin->setTimezone(IST')->subMonths(6);

然后只需将其保存回数据库,而无需手动将其转换回时间戳。

https://laravel.com/docs/5.5/eloquent-mutators#date-mutators

© www.soinside.com 2019 - 2024. All rights reserved.