Created
April 7, 2020 14:36
-
-
Save diloabininyeri/c19b18ac5c2f0ed7f4932c424ee08bee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php /** @noinspection PhpUnhandledExceptionInspection */ | |
| /** | |
| * Class Now | |
| * @author dılo sürücü <[email protected]> | |
| */ | |
| class Now | |
| { | |
| /** | |
| * @var DateTime | |
| */ | |
| private $dateTime; | |
| /** | |
| * Now constructor. | |
| * @throws Exception | |
| */ | |
| public function __construct() | |
| { | |
| $this->dateTime = new DateTime('now'); | |
| } | |
| /** | |
| * @param int $year | |
| * @return Now | |
| * @throws Exception | |
| * @noinspection PhpUnused | |
| */ | |
| public function addYear(int $year): self | |
| { | |
| $this->dateTime->add(new DateInterval('P' . $year . 'Y')); | |
| return $this; | |
| } | |
| /** | |
| * @noinspection PhpUnused | |
| * @param int $month | |
| * @return Now | |
| * @throws Exception | |
| * @noinspection PhpUnused | |
| */ | |
| public function addMonth(int $month):self | |
| { | |
| $this->dateTime->add(new DateInterval('P' . $month . 'M')); | |
| return $this; | |
| } | |
| /** | |
| * @param int $day | |
| * @return $this | |
| * @throws Exception | |
| */ | |
| public function addDay(int $day): self | |
| { | |
| $this->dateTime->add(new DateInterval('P' . $day . 'D')); | |
| return $this; | |
| } | |
| /** | |
| * @noinspection PhpUnused | |
| * @param int $week | |
| * @return $this | |
| * @throws Exception | |
| */ | |
| public function addWeek(int $week): self | |
| { | |
| return $this->addDay($week * 7); | |
| } | |
| /** | |
| * @noinspection PhpUnused | |
| * @param int $second | |
| * @return $this | |
| * @throws Exception | |
| */ | |
| public function addSecond(int $second): self | |
| { | |
| $this->dateTime->add(new DateInterval('PT' . $second . 'S')); | |
| return $this; | |
| } | |
| /** | |
| * @param int $minute | |
| * @return $this | |
| * @throws Exception | |
| */ | |
| public function addMinute(int $minute): self | |
| { | |
| $this->dateTime->add(new DateInterval('PT' . $minute . 'M')); | |
| return $this; | |
| } | |
| /** | |
| * @param int $hour | |
| * @return $this | |
| * @throws Exception | |
| */ | |
| public function addHour(int $hour): self | |
| { | |
| $this->dateTime->add(new DateInterval('PT' . $hour . 'H')); | |
| return $this; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function get(): string | |
| { | |
| return $this->dateTime->format('Y-m-d H:i:s'); | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function __toString() | |
| { | |
| return $this->get(); | |
| } | |
| } | |
| /** | |
| * @return Now | |
| * @throws Exception | |
| */ | |
| function now() | |
| { | |
| return new Now(); | |
| } | |
| //using for example | |
| echo now(); //2020-03-10 22:10 | |
| echo now()->addDay(1); //2020-03-11 22:10 | |
| echo now()->addDay(1)->addHour(1); // //2020-03-11 23:10 | |
| echo now()->addDay(1)->addHour(1)->addMinute(30); // //2020-03-11 23:40 | |
| echo now()->addDay(1)->addHour(1)->addMinute(30)->addSecond(10); // //2020-03-11 23:50 | |
| //or u can use get method for example | |
| echo now()->addDay(1)->addHour(1)->addMinute(30)->get(); // //2020-03-11 23:40 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment