for getting sunday to saturday dates for calender week i am using that code in php :
$dt = strtotime(date('Y-m-d'));
$res['start'] = date('N', $dt)==1 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last sunday', $dt));
$res['end'] = date('N', $dt)==7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('saturday', $dt));
$day_of_week = date('N', strtotime($res['start']));
$given_date = strtotime( $res['end'] );
$first_of_week = date('Y-m-d', strtotime("- {$day_of_week} day", $given_date));
$first_of_week = strtotime($first_of_week);
for($i=1 ;$i<=7; $i++)
{
$datess[] = date('Y-m-d', strtotime("+ {$i} day", $first_of_week));
$week_array[] = date('m/d', strtotime("+ {$i} day", $first_of_week));
print_r($datess);
}
exit;
after this i am getting array from 2018-10-29 to 2018-11-04 but i want array from 2018-11-04 2018-11-10 what i am doing wrong here in this code can anyone please let me know or correct me where i am doing wrong in this code
Are you assuming Sunday is day of the week number 1? And Saturday 7?
If so, Sunday is 7 and Saturday 6. So your code would look like this:
$dt = strtotime(date('Y-m-d'));
$res['start'] = date('N', $dt) == 7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last sunday', $dt));
$res['end'] = date('N', $dt) == 6 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('saturday', $dt));
$day_of_week = date('N', strtotime($res['start']));
$given_date = strtotime($res['end']);
$first_of_week = date('Y-m-d', strtotime("- {$day_of_week} day", $given_date));
$first_of_week = strtotime($first_of_week);
for ($i = 1 ; $i <= 7; $i++) {
$datess[] = date('Y-m-d', strtotime("+ {$i} day", $first_of_week));
$week_array[] = date('m/d', strtotime("+ {$i} day", $first_of_week));
print_r($datess);
}
One solution using PHP powerful date functions time date and strtotime
<?php
if (date('D') == 'Sun') {
$lsTimestamp = time();
} else {
$lsTimestamp = strtotime('last Sunday');
}
$dates = [date('Y-m-d', $lsTimestamp)];
for ($i = 1; $i <= 6; $i++) {
$dates[] = date('Y-m-d', strtotime('+' . $i . ' day', $lsTimestamp));
}
var_dump($dates);
Related
I have days in a month that excluded saturday and sunday. How to divide the range of the days into weeks? If it start with friday, then first week only friday.
you can do it like this
for ($i = 0; $i <= 4; $i++) {
$k = $i - 1;
$from_text = strtotime("this week thursday -$i week");
$to_text = strtotime("-$k week -1 day");
$ymd_week_range = date('Y-m-d', $from_text) . ',' . date('Y-m-d', $to_text);
$day_from = date('j', $from_text);
$day_to = date('j', $to_text);
$month_from = date('M', $from_text);
$month_to = date('M', $to_text);
$year_from = date('Y', $from_text);
$year_to = date('Y', $to_text);
$weeks[$ymd_week_range] = "$month_from $day_from - $month_to $day_to, $year_to";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Sorry for my bad english.
As said in the title , i would to get array of dates in same week(between monday to friday) as a $date
exemple : if $date = 2015-05-05 => i would to have
array that contain : 2015-05-04 , 2015-05-05 ,2015-05-06 2015-05-07, 2015-05-08
Just change the date strings of "2015-05-05" to your desired date.
$day_of_week = date('N', strtotime('2015-05-05'));
$given_date = strtotime("05-05-2015");
$first_of_week = date('Y-m-d', strtotime("- {$day_of_week} day", $given_date));
$first_of_week = strtotime($first_of_week);
for($i=0 ;$i<=7; $i++) {
$week_array[] = date('Y-m-d', strtotime("+ {$i} day", $first_of_week));
}
print_r($week_array);
Date and strtotime are your friends on this:
$myDate = "2015-05-05";
echo date("Y-m-d", strtotime('monday this week', strtotime($myDate)))."\n";
echo date("Y-m-d", strtotime('tuesday this week', strtotime($myDate)))."\n";
echo date("Y-m-d", strtotime('wednesday this week', strtotime($myDate)))."\n";
echo date("Y-m-d", strtotime('thursday this week', strtotime($myDate)))."\n";
echo date("Y-m-d", strtotime('friday this week', strtotime($myDate)))."\n";
Output:
2015-04-05
2015-05-05
2015-06-05
2015-07-05
2015-08-05
Demo:
http://ideone.com/hEem8k
Yet another solution
$my_date = "2012-10-18";
$week = date("W", strtotime($my_date)); // get week
$y = date("Y", strtotime($my_date)); // get year
echo $first_date = date('d-m-Y',strtotime($y."W".$week)); //first date
echo $second_date = date("d-m-Y",strtotime("+1 day", strtotime($first_date)));
you can loop or add +1 to previous date and so on..
This function will return all dates of current week starting from monday to sunday
See Working Demo
function GetCurrentWeekDates()
{
if (date('D') != 'Mon') {
$startdate = date('Y-m-d', strtotime('last Monday'));
} else {
$startdate = date('Y-m-d');
}
//always next saturday
if (date('D') != 'Sat') {
$enddate = date('Y-m-d', strtotime('next Saturday'));
} else {
$enddate = date('Y-m-d');
}
$DateArray = array();
$timestamp = strtotime($startdate);
while ($startdate <= $enddate) {
$startdate = date('Y-m-d', $timestamp);
$DateArray[] = $startdate;
$timestamp = strtotime('+1 days', strtotime($startdate));
}
return $DateArray;
}
print_r(GetCurrentWeekDates());
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 need to get a given week's dates list when a one date of that week and the starting day is given. Starting day may be Monday, Sunday etc.
Here is a way that I think works, not sure it's the most efficient way. $weekStart should be set to the day you consider the first of the week (0 = Sunday, 1 = Monday etc) and $date is the input date.
$weekStart = 0;
$date = '2011-06-1';
$timestamp = strtotime($date);
$dayOfWeek = date('N', $timestamp);
$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart, date('Y', $timestamp));
$endDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + 6 + $weekStart, date('Y', $timestamp));
echo 'Week runs from ' . date('Y-m-d', $startDate) . ' to ' . date('Y-m-d', $endDate);
Replace $dayOfWeek = date('N', $timestamp); with $dayOfWeek = date('w', $timestamp); because if using a Sunday it was returning wrong week dates.
Tested using $date = '2013-07-28'.
$weekStart = 0;
$date = '2011-06-1';
$timestamp = strtotime($date);
$dayOfWeek = date('w', $timestamp);
$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart, date('Y', $timestamp));
$endDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + 6 + $weekStart, date('Y', $timestamp));
echo 'Week runs from ' . date('Y-m-d', $startDate) . ' to ' . date('Y-m-d', $endDate);
i have one date.
example : $date='2011-21-12';
data format :yyyy-dd-mm;
IF date is Saturday or Sunday.
if Saturday add 2 day to the given date.
if Sunday add 1 day to the given date.
?
In a single line of code:
if (date('N', $date) > 5) $nextweekday = date('Y-m-d', strtotime("next Monday", $date));
If the day of week has a value greater than 5 (Monday = 1, Sat is 6 and Sun is 7) set $nextweekday to the YYYY-MM-DD value of the following Monday.
Editing to add, because the date format may not be accepted, you would need to reformat the date first. Add the following lines above my code:
$pieces = explode('-', $date);
$date = $pieces[0].'-'.$pieces[2].'-'.$pieces[1];
This will put the date in Y-m-d order so that strtotime can recognize it.
You can use the date and strtotime functions for this, like so:
$date = strtotime('2011-12-21');
$is_saturday = date('l', $date) == 'Saturday';
$is_sunday = date('l', $date) == 'Sunday';
if($is_saturday) {
$date = strtotime('+2 days', $date);
} else if($is_sunday) {
$date = strtotime('+1 days', $date);
}
echo 'Date is now ' . date('Y-m-d H:i:s', $date);
What about this:
$date='2011-21-12';
if (date("D", strtotime($date)) == "Sat"){
$new_date = date("Y-m-d", strtotime("+ 2 days",$date);
}
else if (date("D", strtotime($date)) == "Sun"){
$new_date = date("Y-m-d", strtotime("+ 1 day",$date);
}
The DateTime object can be really helpful for anything like this.
In this case.
$date = DateTime::createFromFormat('Y-d-m', '2011-21-12');
if ($date->format('l') == 'Sunday')
$date->modify('+1 day');
elseif ($date->format('l') == 'Saturday')
$date->modify('+2 days');
If you want to get the date back in that format.
$date = $date->format('Y-d-m');
$date = '2011-21-12'
$stamp = strtotime($date);
$day = date("l", $stamp);
if ($day == "Saturday"){
$stamp = $stamp + (2*+86400);
}elseif($day == "Sunday"){
$stamp = $stamp + 86400;
}
echo date("Y-d-m", $stamp);
The only reason i can think why this wouldnt work is strtotime not recognising that data format...
For sundays date('w',$date) will return 0, so the simplest test code is:
if(!date('w',strtotime($date)) { ... } //sunday
I just subtracted the difference from 8 an added it to the days.
if(date('N', strtotime($date)) >= 6) {
$n = (8 - date("N",strtotime($date)));
$date = date("Y-m-d", strtotime("+".$n." days");
}