0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

miriwoお一人様Advent Calendar 2022

Day 13

PHP Laravel CarbonImmutableのインスタンスの時間情報を1時間追加する

Last updated at Posted at 2022-12-13

この記事はmiriwoお一人様 Advent Calendar 2022の13日目の記事です

概要

  • CarbonImmutableのインスタンスの時間情報を1時間追加する方法をまとめる

方法

  • インスタンスにaddHour()メソッドで1時間追加する。

    use Carbon\CarbonImmutable;
    
    $dateTime = new CarbonImmutable('2023-01-01');
    echo $dateTime; // 2023-01-01 00:00:00
    
    $dateTimeAddOneHour = $dateTime->addHour();
    echo $dateTimeAddOneHour;// 2023-01-01 01:00:00
    
  • ちなみにsubHour()を使うことで1時間引く事ができる。

    use Carbon\CarbonImmutable;
    
    $dateTime = new CarbonImmutable('2023-01-01');
    echo $dateTime; // 2023-01-01 00:00:00
    
    $dateTimeSubOneHour = $dateTime->subHour();
    echo $dateTimeSubOneHour;// 2022-12-31 23:00:00
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?