is there a solution to find the start and the end date for a specific week number. Example:
If i enter $week = 5, i want to get 2016-03-28 - 2016-04-03. I need only for the current year.
is there a solution to find the start and the end date for a specific week number. Example:
If i enter $week = 5, i want to get 2016-03-28 - 2016-04-03. I need only for the current year.
I think what you're looking for is strtotime and date.
<?php
$year = 2016;
$week = 1;
$a = strtotime('January 1 ' . $year);
$b = date('N', $a) - 1;
$c = $a;
$a += (7 - $b) * 86400;
if($week > 1) {
$start = $a + (($week - 2) * 604800);
$end = $start + ($b ? 518400 : 604800);
}
else {
$start = $c;
$end = $a - 86400;
}
echo date('l jS \of F Y', $start);
echo '<br />';
echo date('l jS \of F Y', $end);
Friday 1st of January 2016
Sunday 3rd of January 2016
As a function with some validation. This version will even keep the last week of the year short, like the first week, if the last week rolls into the next year. Returns false if the week does not fall within the year:
<?php
function weekdays($week, $year = false) {
$week = floor($week);
if($week < 0 || $week > 53)
return false;
if($year == false)
$year = date('Y');
$a = strtotime('January 1 ' . $year);
$b = date('N', $a) - 1;
$c = $a;
$a += (7 - $b) * 86400;
if($week > 1) {
$a += (($week - 2) * 604800);
$b = $a + ($b ? 518400 : 604800);
return
date('Y', $a) == $year
? array(
'first' => $a,
'last' =>
date('Y', $b) > $year
? $b - (8 - date('N', $b)) * 86400
: $b
) : false;
}
else
return array('first' => $c, 'last' => $a - 86400);
}
$week = weekdays(14, 2016);
echo date('l jS \of F Y', $week['first']);
echo '<br />';
echo date('l jS \of F Y', $week['last']);
Monday 28th of March 2016
Sunday 3rd of April 2016
You could use PHP relative datetime functions (http://php.net/manual/en/datetime.formats.relative.php) like:
//date_default_timezone_set('Europe/Amsterdam');
$date = new DateTime();
$date->modify('2016W5'); //First day of week 5 of 2016
$date->modify('2016W5 +6 days'); //Last day of week 5 of 2016
Or build your own function:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
Extracted from here: Finding First day of week via php
You can do this with DateTime:
$year = 2016;
$week = 13;
$date = new DateTime();
$date->setISODate($year, $week);
$start = $date->format('Y-m-d');
$end = $date->modify('+6 days')->format('Y-m-d');
echo 'Week start: '. $start , "\n"; // 2016-03-28
echo 'Week end: '. $end , "\n"; // 2016-04-03
If i enter $week = 5, i want to get 2016-03-28 - 2016-04-03.
So, I think you want monday and sunday of 5th week of march.
To obtain this, I create a simple function that require a date string (YYYY-MM-DD) and the week number. The function create a DateTime
object from given string, then modify it in the first day of month, then retrieve first day of date week and add $week
-1 weeks. At this point we have the first day (monday) of desired week. We clone this date to $end
and we add to it 6 days.
Function returns an object with $start
and $end
dates as properties, obtained casting an array created by compact()
:
function monthWeek( $date, $week )
{
$start = new DateTime( $date );
$start->modify( 'first day of this month' );
$start->modify( '-'.($start->format( 'w' )-1).' days' );
$start->modify( '+'.($week-1).' weeks' );
$end = clone $start;
$end->modify( '+6 days' );
return (object) compact( 'start', 'end' );
}
Calling above function with a casual day of march:
$week = monthWeek( '2016-03-25', 5 );
echo $week->start->format( 'l, Y-m-d' );
echo " => ";
echo $week->end->format( 'l, Y-m-d' );
we obtain this:
Monday, 2016-03-28 => Sunday, 2016-04-03
You can use the function also for retrieve year-based week, using a day of january as first arguments:
$week = monthWeek( '2016-01-25', 14 );
echo $week->start->format( 'l, Y-m-d' );
echo " => ";
echo $week->end->format( 'l, Y-m-d' );
will output:
Monday, 2016-03-28 => Sunday, 2016-04-03