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 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
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
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
phpFiddle demo
Related
How to enlist all the days between for instance 2018-06-04 and 2018-06-10 that between 2018-06-04 - 2018-06-10 those days will be 2018-06-05, 2018-06-06, 2018-06-07, 2018-06-08, 2018-06-09, the same goes for 2018-06-11 - 2018-06-17 and so on...
So far I'ive managed to divide month into week chunks (below), I want to further divide weeks into days...
2018-06-04 - 2018-06-10
2018-06-11 - 2018-06-17
2018-06-18 - 2018-06-24
2018-06-25 - 2018-07-01
http://zakodowac.pl/
This is my PHP code which produces week chunks above 2018-06-04 - 2018-06-10 and so on...:
function getMondays($y, $m) {
return new DatePeriod(
new DateTime("first monday of $y-$m"),
DateInterval::createFromDateString('next monday'),
new DateTime("last day of $y-$m")
);
}
function list_week_days($year, $month) {
foreach (getMondays($year, $month) as $monday) {
echo $monday->format(" Y-m-d\n");
echo '-';
$sunday = $monday->modify('next Sunday');
echo $sunday->format(" Y-m-d\n");
echo '<br>';
}
}
list_week_days(2018, 06);
could you try this:
$begin = strtotime('2018-06-04');
$end = strtotime('2018-06-10');
while($begin < $end){
$begin = $begin +84600;
echo date('Y-m-d', $begin) . ' ';
}
if correctly understood, then the following should yield the results you expect:
// set current date
$date = '04/30/2009';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
print date("m/d/Y l", $ts) . "\n";
}
if you want to be even more clever, you can use:
// set current date
$date = '04/30/2009';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// find the year (ISO-8601 year number) and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
for($i = 1; $i <= 7; $i++) {
// timestamp from ISO week date format
$ts = strtotime($year.'W'.$week.$i);
print date("m/d/Y l", $ts) . "\n";
}
All of which, alongside more information, can be found on this website and credit goes to the author of that post.
I have some pages that require a date to be set in the future (either 2 or 5 days as set by a variable).
This date needs to be only counting Monday - Friday, excluding weekends.
What I have so far ($tts is the variable of 2 or 5 depending on page);
$Today = date('N:m:y');
$NewDate = date('l \t\h\e jS \o\f F',strtotime($Today) + (24*3600*$tts));
$businessDays = [1, 2, 3, 4, 5];
echo $NewDate;
This works without excluding weekend days.
I have tried to use the $businessDays but I'm unsure of how I can use this to count what days are between $Today and $NewDate
Try:
$plusFive = strtotime( '+5 weekday' );
$plusFive = date( 'Y-m-d', $plusFive );
strtotime also takes a second parameter which could be the base for the +5.
Try this code.
It should pick a weekday $tts days ahead.
$Today = date('N:m:y');
$NewDate = date('l \t\h\e jS \o\f F',strtotime($Today . '+' . $tts .' weekdays'));
echo $NewDate;
https://3v4l.org/4SX0m
A DateTime option:
<?php
$start = new DateTime();
date_add( $start, date_interval_create_from_date_string( '5 weekdays' ) );
echo $start->format( 'd-m-Y' );
You could check if its weekend and then add 2 days for Saturday and Sunday like this:
$busDays = 3;
$day = date("w");
if( $day > 2 && $day <= 5 ) { /* if between Wed and Fri */
$day += 2; /* add 2 more days for weekend */
}
$day += $busDays;
How do I compute the start and end date of a week by month, year, and week number? The year is a 4-digit integer, such as 2016; the month is an integer, range 1-12; the week is an integer in the range 1-5.
I already have this code block:
function getStartAndEndDate($week, $year)
{
$time = strtotime("1 $year", time());
$day = date('w', $time);
$time += ((7 * $week) + 1 - $day) * 24 * 3600;
$return[0] = date('Y-n-j', $time);
$time += 6 * 24 * 3600;
$return[1] = date('Y-n-j', $time);
return $return;
}
How can I use or rewrite this function?
As for your function above in the question I have no idea, but assuming the week starts on Monday and ends on Sunday something like this might work
function getFirstandLastDate($year, $month, $week) {
$thisWeek = 1;
for($i = 1; $i < $week; $i++) {
$thisWeek = $thisWeek + 7;
}
$currentDay = date('Y-m-d',mktime(0,0,0,$month,$thisWeek,$year));
$monday = strtotime('monday this week', strtotime($currentDay));
$sunday = strtotime('sunday this week', strtotime($currentDay));
$weekStart = date('d M y', $monday);
$weekEnd = date('d M y', $sunday);
return $weekStart . ' - ' . $weekEnd;
}
echo getFirstandLastDate( 2016, 1, 1 );
I am writing a program code to display the week-number between two dates. I als0 want to display the dates between the week-number. I have written the following code as per the example codes from stack overflow.(here && here)
function getStartAndEndDate($week, $year)
{
$time = strtotime("1 January $year", time());
$day = date('w', $time);
$time += ((7*$week)+1-$day)*24*3600;
$return[0] = date('dMY', $time);
$time += 6*24*3600;
$return[1] = date('dMY', $time);
return $return;
}
$startTime = strtotime('2013-04-01');
$endTime = strtotime('2013-05-03');
$weeks = array();
while ($startTime < $endTime)
{
$weeks[] = date('W', $startTime);
$startTime += strtotime('+1 week', 0);
}
echo count($weeks)."<br/>";
$year="2013";
foreach ($weeks as $key => $w)
{
echo "Week Number:".$w."--";
$return=getStartAndEndDate($w,$year);
$r0=$return[0];
$r1=$return[1];
echo "Start-".$r0."-End-".$r1."<br/>";
}
But the output is wrong. Output shows:
5
Week Number:14--Start-07Apr2013-End-13Apr2013
Week Number:14--Start-07Apr2013-End-13Apr2013
Week Number:15--Start-14Apr2013-End-20Apr2013
Week Number:16--Start-21Apr2013-End-27Apr2013
Week Number:17--Start-28Apr2013-End-04May2013
14th week starts from 31Mar2013 to 6Apr2013. I couldn't figure out what the problem is. Any help must be appreciated!
Did you try to check with date
$startTime = strtotime('2012-12-30');
$endTime = strtotime('2013-05-03');
The actual14th week is
Week Number:14--Start-08Apr2013-End-14Apr2013
not
14th week starts from 31Mar2013 to 6Apr2013
The PHP start weeks from Monday -->Sunday. So if you take 2012-12-30 which is Sunday it will calculate
Week Number:01--Start-07Jan2013-End-13Jan2013
Week Number:02--Start-14Jan2013-End-20Jan2013
Week Number:03--Start-21Jan2013-End-27Jan2013
Week Number:04--Start-28Jan2013-End-03Feb2013
Week Number:05--Start-04Feb2013-End-10Feb2013
Week Number:06--Start-11Feb2013-End-17Feb2013
Week Number:07--Start-18Feb2013-End-24Feb2013
Week Number:08--Start-25Feb2013-End-03Mar2013
Week Number:09--Start-04Mar2013-End-10Mar2013
Week Number:10--Start-11Mar2013-End-17Mar2013
Week Number:11--Start-18Mar2013-End-24Mar2013
Week Number:12--Start-25Mar2013-End-31Mar2013
Week Number:13--Start-01Apr2013-End-07Apr2013
Week Number:14--Start-08Apr2013-End-14Apr2013
I have figured it out:
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);
}
$year="2013";
$startDate="2012-12-30";
$endDate="2013-05-03";
$p = new DatePeriod(
new DateTime($startDate),
new DateInterval('P1W'),
new DateTime($endDate)
);
foreach ($p as $k=>$w) {
echo $k.":".$w->format('W')."<br/>";
$w=$w->format('W');
$sStartDate = week_start_date($w, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
echo $sStartDate."&&".$sEndDate."<br/>";
}
I have made a form where users can select a date from a calander. It wil return d/m/Y, 14/05/2013.
What i need is the Saturday following up that date 14/05/2013 what will be 18/05/2013
The date field is called: $_POST['field_3']
I have been struggling with strtotime but with no succes
I have done sofar:
<?php
$today = $_POST['field_3'];
$date = strtotime('d/m/Y','next Saturday', $today);
$initialString = date('m/d/Y', $date);
$end = date('m/d/Y', strtotime( 'next saturday 11:59 pm', $date));
echo $today ."<br>";
echo $initialString . ' - ' . $end;
?>
which returns:
14/05/2013
01/01/1970 - 01/03/1970
Very basic, but this can help :
<?php
$year = 2013; // use substr() (or other stuff) to set these variables
$month = 5;
$day = 14;
$newDate = mktime(0, 0, 0, $month, $day, $year); // creates a date with previous variables
$dayOfWeek = date('w', $newDate); // get the weekday number; 0 = sunday, ..., 6 = saturday
$numberOfDaysTillNextSaturday = (6 == $dayOfWeek) ? 7 : (6 - $dayOfWeek); // how many days until next saturday ? If saturday = 6, otherwise = (Saturday - weekday)
$nextSaturdayDate = $newDate + (86400 * $numberOfDaysTillNextSaturday); // creates a new date corresponding to next saturday
$nextSaturdayString = date("d/m/Y", $nextSaturdayDate); // formats the new date as (day)/(month)/(year)
echo $nextSaturdayString; // echoes the string
?>