Skip to content
Snippets Groups Projects
Commit 84bd1fab authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

add countFriday13

parent b4966b44
No related branches found
No related tags found
1 merge request!6Ptps controller 6
......@@ -2,6 +2,9 @@
namespace App\Controller;
use DateInterval;
use DatePeriod;
use DateTime;
use DateTimeImmutable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
......@@ -9,17 +12,24 @@ use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
private function howDaysToNy(DateTimeImmutable $date): int {
$endYear = date("Y-12-31", date_timestamp_get($date));
$dateInterval = date_diff(new DateTimeImmutable($endYear), $date);
return $dateInterval->format("%a") + 1;
private function countFriday13(int $year): iterable {
$stardDate = new DateTime("{$year}-01-01 Friday");
$year += 1;
$endDate = new DateTime("{$year}-01-01");
$interval = new DateInterval('P7D');
foreach(new DatePeriod($stardDate, $interval, $endDate) as $day) {
yield new DateTimeImmutable($day->format("Y-m-d"));
}
}
#[Route('/', name: 'home')]
public function home(): Response
{
$count = $this->howDaysToNy(new DateTimeImmutable());
return $this->render('home.html.twig', ['count' => $count]);
#[Route('/{year}', name: 'home')]
public function home(int $year): Response
{
$fridays = array();
foreach($this->countFriday13($year) as $date) {
$fridays[] = $date->format("Y-m-d l");
}
return $this->render('home.html.twig', ['fridays' => $fridays]);
}
}
{% block body %}
<h1>Кол-во дней до НГ: {{ count }}</h1>
<h1>Кол-во пятниц 13 в году: {{ fridays|length }}</h1>
{% for day in fridays %}
<p>{{ day }}</p>
{% endfor %}
{% endblock %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment