How to list start and end dates of a specific week

1

What is desired is the following, a function that receives as a parameter, the week number of a specific year and returns the date on which it starts and ends that week. for example

function weekStartEnd($weekNumber,$year){
  $firstDateOfWeek=date('w',strtotime('???'));
  $lastDateOfWeek=date('w',strtotime('???'));
  return [$firstDateOfWeek,$lastDateOfWeek];
}

The problem is that I do not know how to use strtotime , or mktime to get the fecha de inicio y fin de esa semana .

In summary, a function that can answer the question, when does it start and end week 1 of the year 2018? And that returns as a result

from 01/01/2018 to 06/01/2018

If the solution can be javaScript is also accepted

    
asked by Aquiles Perez 29.12.2017 в 00:14
source

3 answers

1

In case anyone needs it, after so much research, I had to formulate searches in English and they gave me this solution.

function getStartAndEndDate($week, $year)
    {
        //setting the default time zone
      //  date_default_timezone_set('America/New_York');

        //getting the
        //$firstWeek = date('W',strtotime("January 1 $year", date(time())));
        //echo "Year : ".$year."<br/>"."Week : ".$week."<br/>";
        $firstWeekThursDay = date('W',strtotime("January $year first thursday",date(time())));

        if($firstWeekThursDay == "01")
        {
            $time      = strtotime("January $year first thursday",date(time()));
            //echo $time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $time      = ($time-(4*24*3600))+(((7*$week)-6)*24*3600);
            //echo $time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $return[0] = date('Y-m-d', $time);
            $time += 6*24*3600;
            $return[1] = date('Y-m-d', $time);
            //print_r($return);
        }
        else
        {
            $time = strtotime("January 1 $year", time());
            //echo "<br/>".$time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $time      = ($time-(4*24*3600))+(((7*$week)-6)*24*3600);
            //echo $time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $return[0] = date('Y-m-d', $time);
            $time     += 6*24*3600;
            $return[1] = date('Y-m-d', $time);
            //print_r($return);
            //echo "<br/>End of Hi<br/>";

        }
        return $return;
    }
    
answered by 29.12.2017 / 00:36
source
2

If you use object-oriented programming I leave the functions with Datetime:

protected function lastWeekDay($week, $year)
{
    $date = new \DateTime("{$year}-01-01 00:00:00");
    $date->setTimestamp($date->getTimestamp() + $week * 7 * 24 * 60 * 60);
    return $date;
}

protected function firstWeekDay($week, $year)
{
    $lastWeekDay = $this->lastWeekDay($week, $year);
    return $lastWeekDay->setTimestamp($lastWeekDay->getTimestamp() - (86400 * ($lastWeekDay->format('N') - 1)));
}

If you want to use mktime and strtotime: I'll leave you the following:

protected function lastWeekDay($week, $year)
{
    $timestamp = mktime(0, 0, 0, 1, 1, $year) + ($week * 7 * 24 * 60 * 60);
    return date('Y-m-d', $timestamp);
}

protected function firstWeekDay($week, $year)
{
    $timestamp = mktime(0, 0, 0, 1, 1, $year) + ($week * 7 * 24 * 60 * 60);
    $monday = $timestamp - 86400 * (date('N', $timestamp) - 1);
    return date('Y-m-d', $monday);
}

Greetings

    
answered by 29.12.2017 в 01:30
0

You can use the following script of PHP

<?php
$year=2014;
$month=1;
$day=26;

# Obtenemos el numero de la semana
$semana=date("W",mktime(0,0,0,$month,$day,$year));

# Obtenemos el día de la semana de la fecha dada
$diaSemana=date("w",mktime(0,0,0,$month,$day,$year));

# el 0 equivale al domingo...
if($diaSemana==0)
    $diaSemana=7;

# A la fecha recibida, le restamos el dia de la semana y obtendremos el lunes
$primerDia=date("d-m-Y",mktime(0,0,0,$month,$day-$diaSemana+1,$year));

# A la fecha recibida, le sumamos el dia de la semana menos siete y obtendremos el domingo
$ultimoDia=date("d-m-Y",mktime(0,0,0,$month,$day+(7-$diaSemana),$year));

echo "<br>Semana: ".$semana." - año: ".$year;
echo "<br>Primer día ".$primerDia;
echo "<br>Ultimo día ".$ultimoDia;
?>

It can also help you read these two SO responses

Get the date of the first and last day of last week PHP

How can I get the first and last day of a specific week in Javascript

    
answered by 29.12.2017 в 11:59