PHP DateTime Few Usage Cases

New custom date objects can be initialized like this too.

Code

public function testAction(Request $request)
{
    $date = new \DateTime(null);
    $date->format('Y-m-d H:i:s');
    dump($date);
    $date->modify('-1 second');
    dump($date);
    $date->modify('-1 minute');
    dump($date);
    $date->modify('-1 hour');
    dump($date);
    $date->modify('-1 day');
    dump($date);
    $date->modify('-1 month');
    dump($date);
    $date->modify('-1 year');
    dump($date);
    $date->modify('+1 second');
    dump($date);
    $date->modify('+1 minute');
    dump($date);
    $date->modify('+1 hour');
    dump($date);
    $date->modify('+1 day');
    dump($date);
    $date->modify('+1 month');
    dump($date);
    $date->modify('+1 year');
    dump($date);

}

Result

DateTime {#487 ▼
  +"date": "2017-04-27 09:44:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2017-04-27 09:44:43.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2017-04-27 09:43:43.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2017-04-27 08:43:43.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2017-04-26 08:43:43.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2017-03-26 08:43:43.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2016-03-26 08:43:43.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2016-03-26 08:43:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2016-03-26 08:44:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2016-03-26 09:44:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2016-03-27 09:44:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2016-04-27 09:44:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#487 ▼
  +"date": "2017-04-27 09:44:44.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

Code

public function testAction (Request $request)
{
    $date = array(new \DateTime(null));
    $date[] = new \DateTime('-1 second');
    $date[] = new \DateTime('-1 minute');
    $date[] = new \DateTime('-1 hour');
    $date[] = new \DateTime('-1 day');
    $date[] = new \DateTime('-1 month');
    $date[] = new \DateTime('-1 year');
    $date[] = new \DateTime('+1 second');
    $date[] = new \DateTime('+1 minute');
    $date[] = new \DateTime('+1 hour');
    $date[] = new \DateTime('+1 day');
    $date[] = new \DateTime('+1 month');
    $date[] = new \DateTime('+1 year');
    foreach ($date as $d){
        dump($d);
    }
}

Result

DateTime {#487 ▼
  +"date": "2017-04-27 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#496 ▼
  +"date": "2017-04-27 09:43:19.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#495 ▼
  +"date": "2017-04-27 09:42:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#494 ▼
  +"date": "2017-04-27 08:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#499 ▼
  +"date": "2017-04-26 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#498 ▼
  +"date": "2017-03-27 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#591 ▼
  +"date": "2016-04-27 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#592 ▼
  +"date": "2017-04-27 09:43:21.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#593 ▼
  +"date": "2017-04-27 09:44:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#594 ▼
  +"date": "2017-04-27 10:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#595 ▼
  +"date": "2017-04-28 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#596 ▼
  +"date": "2017-05-27 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#597 ▼
  +"date": "2018-04-27 09:43:20.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

Misc Examples:

$startDate = new \DateTime(‘now +1 day’);
$endDate = clone $startDate;
$endDate->modify(‘+2 day’);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s