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";
}
Related
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);
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 );
Suppose i have a month June 2014. Now i want to get dates of all Mondays in June month.
like Monday is coming on following days so answer will be like following
2014-06-02
2014-06-09
2014-06-16
2014-06-23
2014-06-30
please do not give be static solution only for June. I need dynamic solution for every month and purely in PHP.
Try this -
<?php
$startDate = "2014-06-01";
$endDate = "2014-06-30";
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
for($i = strtotime('Monday', $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo date('l Y-m-d', $i).PHP_EOL;
DEMO:
http://3v4l.org/n4ULA
Try to create an array with all your date with day on key (with variable $day and $date):
$array = array("Monday" => "2014-06-02", "Tuesday" => "2014-06-03", "Wednesday" => "2014-06-04");
You create a loop to reach all the result :
foreach($array as $key => $value {
if($key == "Monday")
echo $value;
}
Using the above, I created this so you can use variables to define the month, year, and selected weekday.
$month = "6";
$year = "2022";
$weekday = "Tuesday";
$d=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$first_date_of_month = $year."-".$month."-01";
$last_date_of_month = $year."-".$month."-".$d;
$startDate = strtotime($first_date_of_month);
$endDate = strtotime($last_date_of_month);
for($i = strtotime($weekday, $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo "<br />". date('l Y-m-d', $i).PHP_EOL;
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 5 different schedules for 5 weeks:
first week = Monday to Friday (8am to 5pm) && Rest days on Saturday and Sunday
second week = Monday to Friday (10am to 6pm) && Rest days on Saturday and Sunday
third week = Monday to Friday (11am to 7pm) && Rest days on Saturday and Sunday
fourth week = Monday Rest Day && Tuesday to Saturday (10:30 am to 6:30pm) && Sunday Rest Day
fifth week = Monday Rest Day && Tuesday to Saturday (8:30 am to 5:30pm) && Sunday Rest Day
Base on my calculation array [0],[0] which is Monday of first week is set to April 25, 2011.
I have this code to compute the difference between input date and start date, which is April 25, 2011.
$tdays = floor((strtotime($date2) - strtotime($date1))/86400);
I could now compute my work schedule starting April of 2011 up until February of 2012.
However if I enter a date beyond February 2012, the output is wrong due to leap year. Is there a technique for this?
If you are able to make use of php 5.3 you should use date_diff()
or try something like this :
<?php
function dateDifference($startDate, $endDate)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate)
return false;
$years = date('Y', $endDate) - date('Y', $startDate);
$endMonth = date('m', $endDate);
$startMonth = date('m', $startDate);
// Calculate months
$months = $endMonth - $startMonth;
if ($months <= 0) {
$months += 12;
$years--;
}
if ($years < 0)
return false;
// Calculate the days
$offsets = array();
if ($years > 0)
$offsets[] = $years . (($years == 1) ? ' year' : ' years');
if ($months > 0)
$offsets[] = $months . (($months == 1) ? ' month' : ' months');
$offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) : 'now';
$days = $endDate - strtotime($offsets, $startDate);
$days = date('z', $days);
return array($years, $months, $days);
}
?>