I want an array of containing weeks of current month, How can i get this? I have tried different methods but none of them is working for me..
$month = date('m'); // it should be CURRENT MONTH Of Current Year
$year = date('y'); // Current year
$beg = (int) date('W', strtotime("$year-$month"));
$end = (int) date('W', strtotime("$year-$month"));
I have used the above code but not working..
I want to put those week numbers of current month into an array like
array ('1' => 'first week of march', '2' => '2nd week of march', '3' => '3rd week of march', '4' => '4th week of march', '5' => '5th week of march' ) etc
1,2,3,4,5 in array can be week number
For getting beginning and ending ISO week number use below code:
$beg = (int) date('W', strtotime(date("Y-m-01")));
$end = (int) date('W', strtotime(date("Y-m-t")));
function weeksOfMonth($month, $year){
$num_of_days = date("t", mktime(0,0,0,$month,1,$year));
$lastday = date("t", mktime(0, 0, 0, $month, 1, $year));
$no_of_weeks = 0;
$count_weeks = 0;
while($no_of_weeks < $lastday){
$no_of_weeks += 7;
$count_weeks++;
}
return $count_weeks;
}
echo weeksOfMonth(3, 2017);
After some hard time looking for it, Here is the Answer, Sorry for posting a bit late here. I hope it will help you guys.
public static function getWeeksOfMonth()
{
$currentYear = date('Y');
$currentMonth = date('m');
//Substitue year and month
$time = strtotime("$currentYear-$currentMonth-01");
//Got the first week number
$firstWeek = date("W", $time);
if ($currentMonth == 12)
$currentYear++;
else
$currentMonth++;
$time = strtotime("$currentYear-$currentMonth-01") - 86400;
$lastWeek = date("W", $time);
$weekArr = array();
$j = 1;
for ($i = $firstWeek; $i <= $lastWeek; $i++) {
$weekArr[$i] = 'week ' . $j;
$j++;
}
return $weekArr;
}
The Result will be the weeks of current Month..
Array (
[26] => week 1
[27] => week 2
[28] => week 3
[29] => week 4
[30] => week 5
[31] => week 6
)
Related
This question already has answers here:
Find weekly periods (starting on a Monday) for a month
(3 answers)
Closed 5 years ago.
I need to display a range of dates in corresponding week for selected month.
Suppose the selected values are $month=2 and $year=2017.
The output should show a list of ranged dates for weeks in that particular month selected.
$month = $_GET['month'];
$year = $_GET['year'];
...
Output:
Week 1: 01/02/2017 - 05/02/2017
Week 2: 06/02/2017 - 12/02/2017
Week 3: 13/02/2017 - 19/02/2017
Week 4: 20/02/2017 - 26/02/2017
Week 5: 27/02/2017 - 28/02/2017
Ive look through Split the current month in to weeks in php , Get week number in month from date in PHP? and Split date ranges into corresponding weeks , but none of them working for me.
Thanks.
Here is one method that loops with strtotime.
I add 86400*7 on Unix to add a week.
$month = 2;
$year = 2017;
$week = date("W", strtotime($year . "-" . $month ."-01")); // weeknumber of first day of month
Echo date("d/m/Y", strtotime($year . "-" . $month ."-01")) ." - "; // first day of month
$unix = strtotime($year."W".$week ."+1 week");
While(date("m", $unix) == $month){ // keep looping/output of while it's correct month
Echo date("d/m/Y", $unix-86400) . "\n"; // Sunday of previous week
Echo date("d/m/Y", $unix) ." - "; // this week's monday
$unix = $unix + (86400*7);
}
Echo date("d/m/Y", strtotime("last day of ".$year . "-" . $month)); //echo last day of month
https://3v4l.org/LAMBl
Here is your solution
$month = $_GET['month'];
if($month<10)$month = '0'.$month;
$year = $_GET['year'];
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year);
$start = 1; $end = 5;
function get_week_array($start,$end){
global $month, $year,$days;
for($i=0;$i<$end;$i++){
if($start<10)$array[] = '0'.$start.'/'.$month.'/'.$year;
else $array[] = $start.'/'.$month.'/'.$year;
$start = $start+1;
if($start==$days+1)break;
}
return $array;
}
$total = 0;
while($days>$total){
$week[] = get_week_array($start,$end);
$total = $total+$end;
$start = $total+1;
$end = 7;
}
echo "<pre>";print_r($week);
Output
Array
(
[0] => Array
(
[0] => 01/02/2017
[1] => 02/02/2017
[2] => 03/02/2017
[3] => 04/02/2017
[4] => 05/02/2017
)
[1] => Array
(
[0] => 06/02/2017
[1] => 07/02/2017
[2] => 08/02/2017
[3] => 09/02/2017
[4] => 10/02/2017
[5] => 11/02/2017
[6] => 12/02/2017
)
[2] => Array
(
[0] => 13/02/2017
[1] => 14/02/2017
[2] => 15/02/2017
[3] => 16/02/2017
[4] => 17/02/2017
[5] => 18/02/2017
[6] => 19/02/2017
)
[3] => Array
(
[0] => 20/02/2017
[1] => 21/02/2017
[2] => 22/02/2017
[3] => 23/02/2017
[4] => 24/02/2017
[5] => 25/02/2017
[6] => 26/02/2017
)
[4] => Array
(
[0] => 27/02/2017
[1] => 28/02/2017
)
)
Try this:
<?php
$month = 12;
$year = 2017;
$date = new DateTime($year.'-'.$month.'-01');
$count = 1;
$week[$count]['start'] = $date->format('Y-m-d');
while (1) {
$date->modify('+1 day');
if ($date->format('m') != $month) {
$week[$count]['end'] = $date->format('Y-m-d');
break;
}
if ($date->format('D') === 'Sun') {
$week[$count++]['end'] = $date->format('Y-m-d');
}
if ($date->format('D') === 'Mon') {
$week[$count]['start'] = $date->format('Y-m-d');
}
}
print_r($week);
This method can show weeks including day of week to break that week
Show test
$month = '2';
$year = '2017';
$lastDayOfWeek = '7'; //1 (for monday) to 7 (for sunday)
function getWeeksInMonth($year, $month, $lastDayOfWeek)
{
$aWeeksOfMonth = [];
$date = new DateTime("{$year}-{$month}-01");
$iDaysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$aOneWeek = [$date->format('Y-m-d')];
$weekNumber = 1;
for ($i = 1; $i <= $iDaysInMonth; $i++)
{
if ($lastDayOfWeek == $date->format('N') || $i == $iDaysInMonth)
{
$aOneWeek[] = $date->format('Y-m-d');
$aWeeksOfMonth[$weekNumber++] = $aOneWeek;
$date->add(new DateInterval('P1D'));
$aOneWeek = [$date->format('Y-m-d')];
$i++;
}
$date->add(new DateInterval('P1D'));
}
return $aWeeksOfMonth;
}
$weeks = getWeeksInMonth($year, $month, $lastDayOfWeek);
foreach($weeks as $weekNumber => $week){
echo "Week {$weekNumber}: {$week[0]} - {$week[1]}\r\n";
}
Output
Week 1: 2017/02/01 - 2017/02/05
Week 2: 2017/02/06 - 2017/02/12
Week 3: 2017/02/13 - 2017/02/19
Week 4: 2017/02/20 - 2017/02/26
Week 5: 2017/02/27 - 2017/02/28
I want to store names of the week days in an array from given date range.
Eg. If given date range is from 2016-12-1 to 2017-02-22 then i want to store all weekdays comes in this date range.
$timestamp = strtotime('next Sunday');
$days = array();
for ($i = 0; $i < 7; $i++) {
$days[] = strftime('%A', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
Try this. Provide Start date,end date, weekdays as input params
function progDateRange($date, $end_date, array $wkDays, array $excluded)
{
$dates = array();
$current_date = strtotime($date);
$max_date = min(strtotime('+2 years'), strtotime($end_date));
$dow = array_keys($wkDays);
while ($current_date < $max_date) {
if ($excluded && in_array($date_formatted, $excluded, true)) {
continue;
}
if (in_array(date('l'), $dow, true)) {
array_push($dates, $date);
}
$current_date = strtotime('+1 day', $current_date);
$date = date('Y-m-d', $current_date);
}
return $dates;
}
The solution using date and strtotime functions:
$date_from = '2016-12-1';
$date_to = '2017-02-22';
$current = strtotime($date_from);
$max_time = strtotime($date_to);
$weekdays = [];
while ($current <= $max_time) {
// getting weekday name for current timestamp within date range
$weekdays[] = date('l', $current);
$current = strtotime('+1 day', $current); // setting timestamp for each next next day in increase order
}
print_r($weekdays);
The output will be like below:
Array
(
[0] => Thursday
[1] => Friday
[2] => Saturday
[3] => Sunday
[4] => Monday
[5] => Tuesday
[6] => Wednesday
[7] => Thursday
[8] => Friday
[9] => Saturday
[10] => Sunday
[11] => Monday
[12] => Tuesday
[13] => Wednesday
[14] => Thursday
....
...
I try to store next 6 days from current day ,but I'm little stuck here how to store all next 6days in $weekOfdays .Is there any simple function to do that?
<?php
$weekOfdays = array();
$day = date('l');
$weekOfdays[] = $day;
$day = strtotime($day);
$next = strtotime("+6 day",$day);
$weekOfdays[] = date("l",$next);
var_dump($weekOfdays);
// output
// array (size=2)
// 0 => string 'Monday' (length=6)
// 1 => string 'Sunday' (length=6)
?>
I want to see array is like this
array (size=7)
0 => string 'Monday' (length=6)
1 => string 'Tuesday' (length=7)
2 => string 'Wednesday' (length=9)
3 => string 'Thursday' (length=8)
4 => string 'Friday' (length=6)
5 => string 'Saturday' (length=8)
6 => string 'Sunday' (length=6)
Here's one that doesn't directly rely on doing the math on your own:
$days = [];
$period = new DatePeriod(
new DateTime(), // Start date of the period
new DateInterval('P1D'), // Define the intervals as Periods of 1 Day
6 // Apply the interval 6 times on top of the starting date
);
foreach ($period as $day)
{
$days[] = $day->format('l');
}
There are many way some of sample are mentioned as below:
1) using strtotime function and temp $date variable in loop
$date = date('Y-m-d'); //today date
$weekOfdays = array();
for($i =1; $i <= 7; $i++){
$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$weekOfdays[] = date('l : Y-m-d', strtotime($date));
}
print_r($weekOfdays);
2) using strtotime function and +$i days from current date
$date = date('Y-m-d'); //today date
$weekOfdays = array();
for($i =1; $i <= 7; $i++){
$weekOfdays[] = date('l : Y-m-d', strtotime("+$i day", strtotime($date)));
}
print_r($weekOfdays);
3) using DateTime class and modify method
$date = date('Y-m-d'); //today date
$weekOfdays = array();
$date = new DateTime($date);
for($i=1; $i <= 7; $i++){
$date->modify('+1 day');
$weekOfdays[] = $date->format('l : Y-m-d');
}
print_r($weekOfdays);
4) using DateTime class and DateInterval class
$date = date('Y-m-d'); //today date
$weekOfdays = array();
$date = new DateTime($date);
for($i=1; $i <= 7; $i++){
$date->add(new DateInterval('P1D'));
$weekOfdays[] = $date->format('l : Y-m-d');
}
print_r($weekOfdays);
5) using DatePeriod, DateInterval and DateTime class
$date = date('Y-m-d', strtotime('+1 day')); //tomorrow date
$weekOfdays = array();
$begin = new DateTime($date);
$end = new DateTime($date);
$end = $end->add(new DateInterval('P7D'));
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $dt){
$weekOfdays[] = $dt->format('l : Y-m-d');
}
print_r($weekOfdays);
Today is Tuesday, 12 April so output of all code will be:
Array
(
[0] => Wednesday : 2016-04-13
[1] => Thursday : 2016-04-14
[2] => Friday : 2016-04-15
[3] => Saturday : 2016-04-16
[4] => Sunday : 2016-04-17
[5] => Monday : 2016-04-18
[6] => Tuesday : 2016-04-19
)
For more detail have a look at:
strtotime
DateTime
DateInterval
DatePeriod
I believe you need a for loop:
$weekOfdays = array();
$day = date('l');
$weekOfdays[] = $day;
$day = strtotime($day);
for($i = 1; $i <= 6; ++$i) {
$next = strtotime("+$i day",$day);
$weekOfdays[] = date("l",$next);
}
Just use a while loop to adjust day by day. Just add another day +1:
$weekOfdays = array();
$date = time();
$next = strtotime('+6 days');
while ($date <= $next) { // loop until next six
$weekOfdays[] = date('l', $date); // push the day name
$date = strtotime('+1 day', $date); // add +1 on $date
}
print_r($weekOfdays);
Using strtotime function and while-loop
$weekOfdays = array();
$current_date = time(); //get current date.
$next = strtotime('+6 days');
while ($current_date <= $next) {
$weekOfdays[] = date('l', $current_date);
$current_date = strtotime('+1 day', $current_date); // add next date
}
echo "<pre>";
print_r($weekOfdays);
echo "</pre>";
Result :
Array
(
[0] => Friday
[1] => Saturday
[2] => Sunday
[3] => Monday
[4] => Tuesday
[5] => Wednesday
[6] => Thursday
)
here is simple modification in you code which will display total days
<?php
$weekOfdays = array();
$day = date('l');
$weekOfdays[] = $day;
$day = strtotime($day);
$total_days='5';//you can increase or decrease the day to display
for($i=1;$i<=$total_days;$i++){
$next = strtotime("+$i day",$day);
$weekOfdays[] = date("l",$next);
}
print_r($weekOfdays);
?>
And its out put
Array (
[0] => Monday
[1] => Tuesday
[2] => Wednesday
[3] => Thursday
[4] => Friday
[5] => Saturday )
$weekOfdays = array();
$date = date("l");// current day
for($i = 0; $i < 7; $i++) {
$weekOfdays[] = date("l", strtotime($date . "+$i day"));
}
var_dump($weekOfdays);
Check the following.Here array gets 7 day name including the current day.
$weekOfdays = array();
for($i = 1,$nxtday = time(); $i<=7; $i++)
{
$weekOfdays[] = date("l", $nxtday);
$nxtday += 86400;
}
var_dump($weekOfdays);
<?php
$weekOfdays = array();
$currentDay = time();
for ($i = 0; $i < 7; $i++) {
$weekOfdays[] = date('l', $currentDay);
$currentDay = strtotime('+1 day', $currentDay);
}
var_dump($weekOfdays);
will output (today's friday)
array (size=7)
0 => string 'Friday' (length=6)
1 => string 'Saturday' (length=8)
2 => string 'Sunday' (length=6)
3 => string 'Monday' (length=6)
4 => string 'Tuesday' (length=7)
5 => string 'Wednesday' (length=9)
6 => string 'Thursday' (length=8)
<?php
$weekOfDays = array();
$now = time();
$daysCnt = 6;
while ($daysCnt > 0) {
$now = mktime(0,0,0,date('m',$now),date('d',$now)+1,date('Y',$now));
$weekOfDays[] = date('l', $now);
$daysCnt--;
}
print_r($weekOfDays);
output:
Array
(
[0] => Saturday
[1] => Sunday
[2] => Monday
[3] => Tuesday
[4] => Wednesday
[5] => Thursday
)
the purpose of usage mktime() instead of strtotime() is a feature:
<?php
date_default_timezone_set("Europe/Moscow");
$today = date("d-m-Y",mktime(0,0,0,4,28,2013)); // 28-04-2013, sunday
$lastWeek = strtotime("last week $today");
$thisWeek = strtotime("this week $today");
$nextWeek = strtotime("next week $today");
echo "last week - ".$lastWeek." / ".date("d-m-Y H:i\n",$lastWeek);
echo "this week - ".$thisWeek." / ".date("d-m-Y H:i\n",$thisWeek);
echo "next week - ".$nextWeek." / ".date("d-m-Y H:i\n",$nextWeek);
result:
last week - 1366574400 / 22-04-2013 00:00; expected 15-04-2013
this week - 1367179200 / 29-04-2013 00:00; expected 22-04-2013
next week - 1367784000 / 06-05-2013 00:00; expected 29-04-2013
I recommend double-check expected results when you use strtotime()
I have a number of variables that store a year, month and series of dates for that month (there are 2 of these for 2 separate months). I then need to incorporate these into what I believe is a multidimensional array (haven't worked with these types of arrays before). Here's my code that has the variables:
// Set the default timezone
date_default_timezone_set('Australia/Sydney');
$month1 = date('m');
$year1 = date('Y');
$dates1 = '3 5 6 10 12 13 17 19 20 24 26 27 31';
$month2 = date('m', strtotime('first day of next month')) ;
$year2 = date('Y', strtotime('first day of next month')) ;
$dates2 = '10 15 26';
Using Dec 10, 2013 as the current date and the above list of dates I then need to end up with an array in this format:
array("year" => array("month" => array(days)));
that would look like this:
$daysArray = array ("2013" => array("12" => array(3,5,6,10,12,13,17,19,20,24,26,27,31)), "2014" => array("1" => array(10,15,26)));
I'm not sure how to convert these 6 variables into a multidimensional array?
Considering a few edge cases as well (same year etc), i think this is a rather simple (readable) solution:
// Sample data
$month1 = 12;
$year1 = 2013;
$dates1 = '3 5 6 10 12 13 17 19 20 24 26 27 31';
$month2 = 1;
$year2 = 2014;
$dates2 = '10 15 26';
$result = combineDateArrays(createDateArray($year1, $month1, $dates1), createDateArray($year2, $month2, $dates2));
function createDateArray($year, $month, $dates) {
return array($year=>array($month=>explode(" ", $dates)));
}
function combineDateArrays($dateArray1, $dateArray2) {
foreach($dateArray2 as $year=>$months) {
foreach($months as $month=>$days) {
if (!isset($dateArray1[$year])) $dateArray1[$year] = array();
$dateArray1[$year][$month] = $days;
}
}
return $dateArray1;
}
You can create an array from a string using explode:
$daysArray = array($year1 => $month1 => explode(' ', $dates1), $year2 => $month2 => explode(' ', $dates2));
If I have two dates 20-4-2010 and 22-4-2010
in two text box and
I want the dates to be like this 20, 21, 22. How do I get that ?
I am pretty sure this has been answered a quadrillion times before, but anyway:
$start = strtotime('20-04-2010 10:00');
$end = strtotime('22-04-2010 10:00');
for($current = $start; $current <= $end; $current += 86400) {
echo date('d-m-Y', $current);
}
The 10:00 part is to prevent the code to skip or repeat a day due to daylight saving time.
By giving the number of days:
for($i = 0; $i <= 2; $i++) {
echo date('d-m-Y', strtotime("20-04-2010 +$i days"));
}
With PHP5.3
$period = new DatePeriod(
new DateTime('20-04-2010'),
DateInterval::createFromDateString('+1 day'),
new DateTime('23-04-2010') // or pass in just the no of days: 2
);
foreach ( $period as $dt ) {
echo $dt->format( 'd-m-Y' );
}
You can use mktime().
mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.
If you increment the day number you get a valid date back, even if you go past the end of the month:
<?php
$day= 25;
$dateEnd = mktime(0,0,0,5,3,2010);
do {
$dateCur = mktime(0,0,0,4,$day,2010);
$day++;
print date( 'd-m-y', $dateCur) .'<br>';
} while ($dateCur < $dateEnd);
Output:
25-04-10
26-04-10
27-04-10
28-04-10
29-04-10
30-04-10
01-05-10
02-05-10
03-05-10
You can do:
$start = strtotime("2010-04-20"); // get timestamp for start date.
$end = strtotime("2010-04-22"); // get timestamp for end date.
// go from start timestamp to end timestamp adding # of sec in a day.
for($t=$start;$t<=$end;$t+=86400) {
// get the date for this timestamp.
$d = getdate($t);
// print the date.
echo $d['mday'].'-'.$d['mon'].'-'.$d['year']."\n";
}
Output:
20-4-2010
21-4-2010
22-4-2010
/**
* Function to list of weeks start and end.
* #param string strDateFrom (Date From "YYYY-MM-DD")
* #param string strDateTo (Date To "YYYY-MM-DD")
* #return array weeks
*/
function getWeeksBetweenTowDates($date_from, $date_to) {
$startweek = $current_week = date("W", strtotime($date_from));
$endweek = date("W", strtotime($date_to));
$current_year = date("Y", strtotime($date_from));
$current_yearweek = date("Y", strtotime($date_from)) . $startweek;
$end_yearweek = date("Y", strtotime($date_to)) . $endweek;
$start_day = 0;
$end_day = 0;
while ($current_yearweek <= $end_yearweek) {
$dto = new DateTime();
if ($start_day == 0) {
$start_day = $dto->setISODate(date("Y", strtotime($date_from)), $current_week, 0)->format('Y-m-d');
$end_day = $dto->setISODate(date("Y", strtotime($date_from)), $current_week, 6)->format('Y-m-d');
} else {
$start_day = $dto->setISODate(date("Y", strtotime($end_day)), $current_week, 0)->format('Y-m-d');
$end_day = $dto->setISODate(date("Y", strtotime($end_day)), $current_week, 6)->format('Y-m-d');
}
$arr_weeks[sprintf("%02d", $current_week)] = $start_day . " =>" . $end_day;
$last_yearweek_in_year = $current_year . date("W", strtotime('31-12-' . $current_year));
if ($current_yearweek == $last_yearweek_in_year) { //last week in the year
$current_week = 1;
$current_year = ($current_year + 1);
} else {
$current_week = ($current_week + 1);
}
$current_yearweek = $current_year . sprintf("%02d", $current_week);
}
return $arr_weeks;
}
Run Like : date_from=2015-10-20 , date_to=2016-04-15
$arr_weeks = $this->getWeeksBetweenTowDates($date_from, $date_to);
Output:
Array
(
[43] => 2015-10-18 =>2015-10-24
[44] => 2015-10-25 =>2015-10-31
[45] => 2015-11-01 =>2015-11-07
[46] => 2015-11-08 =>2015-11-14
[47] => 2015-11-15 =>2015-11-21
[48] => 2015-11-22 =>2015-11-28
[49] => 2015-11-29 =>2015-12-05
[50] => 2015-12-06 =>2015-12-12
[51] => 2015-12-13 =>2015-12-19
[52] => 2015-12-20 =>2015-12-26
[53] => 2015-12-27 =>2016-01-02
[01] => 2016-01-03 =>2016-01-09
[02] => 2016-01-10 =>2016-01-16
[03] => 2016-01-17 =>2016-01-23
[04] => 2016-01-24 =>2016-01-30
[05] => 2016-01-31 =>2016-02-06
[06] => 2016-02-07 =>2016-02-13
[07] => 2016-02-14 =>2016-02-20
[08] => 2016-02-21 =>2016-02-27
[09] => 2016-02-28 =>2016-03-05
[10] => 2016-03-06 =>2016-03-12
[11] => 2016-03-13 =>2016-03-19
[12] => 2016-03-20 =>2016-03-26
[13] => 2016-03-27 =>2016-04-02
[14] => 2016-04-03 =>2016-04-09
[15] => 2016-04-10 =>2016-04-16
)
Try this, Hope it help helps
$begin = date("Y-m-d", strtotime($date);
$end = date("Y-m-d", strtotime($date));
$begin = new DateTime($begin);
$end = new DateTime($end);
for ($i = $begin; $i <= $end; $i=$i->modify('+1 day')) {
echo $i->format('Y-m-d');
}