<?php declare(strict_types=1);
namespace PracticalTasks;
class Date
{
private $date;
public function __construct($date = null)
{
if ($date) {
$this->date = $date;
} else {
$this->date = date('Y-m-d');
}
}
public function getDay(): string
{
return date('d', strtotime($this->date));
}
public function getMonth($lang = null): string
{
if ($lang) {
if ($lang === 'ru') {
$monthsList = [
'01' => 'Январь',
'02' => 'Февраль',
'03' => 'Март',
'04' => 'Апрель',
'05' => 'Май',
'06' => 'Июнь',
'07' => 'Июль',
'08' => 'Август',
'09' => 'Сентябрь',
'10' => 'Октябрь',
'11' => 'Ноябрь',
'12' => 'Декабрь'
];
$month = date('m', strtotime($this->date));
return str_replace($month, $monthsList[$month], $month);
}
if ($lang === 'en') {
return date('F', strtotime($this->date));
}
}
return date('m', strtotime($this->date));
}
public function getWeekDay($lang = null)
{
if ($lang) {
if ($lang === 'ru') {
$monthsList = [
1 => 'Понедельник',
2 => 'Вторник',
3 => 'Среда',
4 => 'Четверг',
5 => 'Пятница',
6 => 'Суббота',
7 => 'Воскресенье',
];
$weekDay = date('N', strtotime($this->date));
return str_replace($weekDay, $monthsList[$weekDay], $weekDay);
}
if ($lang === 'en') {
return date('l', strtotime($this->date));
}
}
return date('N', strtotime($this->date));
}
public function getYear(): string
{
return date('Y', strtotime($this->date));
}
public function addDay(int $quantityDay): self
{
$this->date = date('Y-m-d', strtotime($this->date . "+$quantityDay day"));
return $this;
}
public function subDay(int $quantityDay): self
{
$this->date = date('Y-m-d', strtotime($this->date . "-$quantityDay day"));
return $this;
}
public function addMonth(int $quantityMonth): self
{
$this->date = date('Y-m-d', strtotime($this->date . "+$quantityMonth month"));
return $this;
}
public function subMonth(int $quantityMonth): self
{
$this->date = date('Y-m-d', strtotime($this->date . "-$quantityMonth month"));
return $this;
}
public function addYear(int $quantityYear): self
{
$this->date = date('Y-m-d', strtotime($this->date . "+$quantityYear year"));
return $this;
}
public function subYear(int $quantityYear): self
{
$this->date = date('Y-m-d', strtotime($this->date . "+$quantityYear year"));
return $this;
}
public function customFormat(string $format)
{
return date($format, strtotime($this->date));
}
public function __toString()
{
return (string)$this->date;
}
}