I would like to know how I can make a select with every Friday of the year in mysql for example

Asked

Viewed 126 times

1

I’m developing a calendar, and it will be used to schedule meetings. There are sectors, such as the T.I sector, which meets every Friday from 9:30 to 11:30, throughout the year. I need to somehow insert in my table "start date" (of the meeting), every Friday of the year.

I’m using the fullCalendar.io, mysql and PHP library. Please, someone give me a light.

1 answer

1


You can build an algorithm that gets you every Friday of the year and then enter the dates in the database. Follow an example in PHP that gets the dates:

<?php
# Ano a qual deseja obter todas as sexta-feiras.
$ano = 2018;

# Iniciamos a variável data com o primeiro dia do ano.
$data = new DateTime("$ano-01-01 08:30");

# Pecorremos todo o ano para encontrar todas as sexta-feiras.
while ($data->format('Y') == $ano)
{
    # Se sexta-feira
    if ($data->format('w') == 5) 
    {
        # Data sexta-feira.
        echo $data->format('Y-m-d H:i');

        # Depois que descobrimos a primeira sexta-feira do ano. Basta adicionar 7 dias a data
        # para encontrar a próxima sexta feira.
        $data->add(new DateInterval('P7D'));
    } 
    else 
    {
        # Caso a data inicial não seja uma sexta. Adiciona mais uma dia a data.
        $data->add(new DateInterval('P1D'));
    }
}
  • Thank you very much brother !!!!

  • One more question, if it’s not too much. How would I add the time of 08:30 for example?

  • @Diegomarcelo I changed the date to be with the fixed time 08:30. Just inform the time on the start date. And when getting the formatted date add the mask to display time and minutes.

  • It was worth once again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.