php function for get all mondays within date range - php

Example:
$startDate is Monday 2007-02-05 and $endDate is Tuesday 2007-02-20. Then I want it to list:
Monday 2007-02-05
Monday 2007-02-12
Monday 2007-02-19
I looked at the PHP manual and found this to get all the days between two dates. But how to do it the way i want? PHP Code:

Rather than get all days and loop through them all, get the first Monday after the start date and then iterate 7 days at a time:
$endDate = strtotime($endDate);
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
echo date('l Y-m-d', $i);

I needed the same and created a simple method.
public function getMondaysInRange($dateFromString, $dateToString)
{
$dateFrom = new \DateTime($dateFromString);
$dateTo = new \DateTime($dateToString);
$dates = [];
if ($dateFrom > $dateTo) {
return $dates;
}
if (1 != $dateFrom->format('N')) {
$dateFrom->modify('next monday');
}
while ($dateFrom <= $dateTo) {
$dates[] = $dateFrom->format('Y-m-d');
$dateFrom->modify('+1 week');
}
return $dates;
}
Then use it.
$dateFromString = '2007-02-05';
$dateToString = '2007-02-20';
var_dump($this->getMondaysInRange($dateFromString, $dateToString));
Result:
array (size=3)
0 => string '2007-02-05' (length=10)
1 => string '2007-02-12' (length=10)
2 => string '2007-02-19' (length=10)
Maybe it will be helpful for somebody.

You can use below function to get a array of dates between a date range of specific day.
You have to input start date, end date and day number in number.The day number is as follow.
1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday. 5 = Friday, 6 = Saturday, 7 = Sunday.
function getDateForSpecificDayBetweenDates($startDate,$endDate,$day_number){
$endDate = strtotime($endDate);
$days=array('1'=>'Monday','2' => 'Tuesday','3' => 'Wednesday','4'=>'Thursday','5' =>'Friday','6' => 'Saturday','7'=>'Sunday');
for($i = strtotime($days[$day_number], strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
$date_array[]=date('Y-m-d',$i);
return $date_array;
}

for ($i = strtotime($startDate); $i <= strtotime($endDate); $i = strtotime('+1 day', $i)) {
if (date('N', $i) == 1) //Monday == 1
echo date('l Y-m-d', $i); //prints the date only if it's a Monday
}

i Create A class, You get All Days In range Date Group By Name of Day:
class DayHelper{
const MONDAY = 'Mon';
const TUESDAY = 'Tue';
const WEDENSDAY = 'Wed';
const THURSDAY = 'Thu';
const FRIDAY = 'Fri';
const SATURDAY = 'Sat';
const SUNDAY = 'Sun';
public function GetYeardays($dateStart, $dateend){
$period = new \DatePeriod(
new \DateTime($dateStart), new \DateInterval('P1D'), (new \DateTime($dateend))
);
$dates = iterator_to_array($period);
$arrayreturn = array();
foreach ($dates as $val) {
$date = $val->format('Y-m-d'); //format date
$get_name = date('l', strtotime($date)); //get week day
$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
switch ($day_name) {
case self::MONDAY:
$MONDAY[] = $date;
$arrayreturn[self::MONDAY] = $MONDAY;
break;
case self::TUESDAY:
$TUESDAY[] = $date;
$arrayreturn[self::TUESDAY] = $TUESDAY;
break;
case self::WEDENSDAY:
$WEDENSDAY[] = $date;
$arrayreturn[self::WEDENSDAY] = $WEDENSDAY;
break;
case self::THURSDAY:
$THURSDAY[] = $date;
$arrayreturn[self::THURSDAY] = $THURSDAY;
break;
case self::FRIDAY:
$FRIDAY[] = $date;
$arrayreturn[self::FRIDAY] = $FRIDAY;
break;
case self::SATURDAY:
$SATURDAY[] = $date;
$arrayreturn[self::SATURDAY] = $SATURDAY;
break;
case self::SUNDAY:
$SUNDAY[] = $date;
$arrayreturn[self::SUNDAY] = $SUNDAY;
break;
}
}
return $arrayreturn;
}
}
The Output will be like this
array (size=7)
'Fri' =>
array (size=5)
0 => string '2016/01/01' (length=10)
1 => string '2016/01/08' (length=10)
2 => string '2016/01/15' (length=10)
3 => string '2016/01/22' (length=10)
4 => string '2016/01/29' (length=10)
'Sat' =>
array (size=5)
0 => string '2016/01/02' (length=10)
1 => string '2016/01/09' (length=10)
2 => string '2016/01/16' (length=10)
3 => string '2016/01/23' (length=10)
4 => string '2016/01/30' (length=10)
'Sun' =>
array (size=4)
0 => string '2016/01/03' (length=10)
1 => string '2016/01/10' (length=10)
2 => string '2016/01/17' (length=10)
3 => string '2016/01/24' (length=10)
'Mon' =>
array (size=4)
0 => string '2016/01/04' (length=10)
1 => string '2016/01/11' (length=10)
2 => string '2016/01/18' (length=10)
3 => string '2016/01/25' (length=10)
'Tue' =>
array (size=4)
0 => string '2016/01/05' (length=10)
1 => string '2016/01/12' (length=10)
2 => string '2016/01/19' (length=10)
3 => string '2016/01/26' (length=10)
'Wed' =>
array (size=4)
0 => string '2016/01/06' (length=10)
1 => string '2016/01/13' (length=10)
2 => string '2016/01/20' (length=10)
3 => string '2016/01/27' (length=10)
'Thu' =>
array (size=4)
0 => string '2016/01/07' (length=10)
1 => string '2016/01/14' (length=10)
2 => string '2016/01/21' (length=10)
3 => string '2016/01/28' (length=10)

I made some changes to response https://stackoverflow.com/a/37300272/6871295
Then I can get the days between dates for any day and return format.
public function getWeekDayInRange($weekday, $dateFromString, $dateToString, $format = 'Y-m-d')
{
$dateFrom = new \DateTime($dateFromString);
$dateTo = new \DateTime($dateToString);
$dates = [];
if ($dateFrom > $dateTo) {
return $dates;
}
if (date('N', strtotime($weekday)) != $dateFrom->format('N')) {
$dateFrom->modify("next $weekday");
}
while ($dateFrom <= $dateTo) {
$dates[] = $dateFrom->format($format);
$dateFrom->modify('+1 week');
}
return $dates;
}

This is code for fetching the weekday of "$startdate" and counting the number of weekdays between two dates.
`$startdate` = '2015-03-01';
`$endate` = '2015-03-31';
`$recurringDay` = date('N', strtotime($startdate)); // recurring Day from date i.e monday = 1, Tuesday = 2 ...etc
$begin = new DateTime(`$startdate`);
$end = new DateTime(date('Y-m-d',strtotime('+1 day', strtotime($endate))));
while($begin format('Y-m-d');
$day[] = $begin->format('N');
$begin->modify('+1 day');
}
$c=0; // counter starts
foreach($day as $key=>$dt) {
if ($dt==`$recurringDay`) // compare it
{
$k[] = $key;
$c++;
}
}
`$nofDays` = $c; // number of mondays , tuesday
foreach($k as $pp) {
//adding session code
`$recurringDatetime[]` = $period[$pp]; // recurring dates
}
print_r(`$recurringDatetime`); // array of dates of monday, tuesday ..etc

$dates = array();
$dates[] = strtotime($start);
for($i = 0; $i <= 12; $i++){
$dates[] = strtotime('+1 week', $dates[$i]);
}
foreach($dates as $date){ echo date("d.m.Y", $date); }
I had similar issue and courses can start on any day. This script picks starting day and collect next days every week until the wanted amount (12 in this case).

Convert $startDate and $endDate before that to timestamps:
foreach ($date = $startDate; $date <= $endDate; $date += 60 * 60 * 24) {
if (strftime('%w', $date) == 1) {
$mondays[] = strftime('%A %Y-%m-%d', $date);
}
}

simply you can add as,
$date_from = "2007-02-05";
$date_from = strtotime($date_from);
$date_to="2007-02-20";
$date_to = strtotime($date_to);
for ($i=$date_from; $i<=$date_to; $i+=86400) {
$day = date("Y-m-d", $i);
$unixTimestamp = strtotime($day);
$dayOfWeek = date("l", $unixTimestamp);
if ($dayOfWeek == "Monday") {
echo $day ."is a". $dayOfWeek;
}
}//end for

Related

How to retrieve the date of the working days in a php array?

Can someone help me, where should I put my $days[] array so that holidays are not included in this array.
My code is as follows
$data['ferie'] = $this->CalendrierModel->find('ferie');
$timestamp = strtotime ('2020-05-18');
$days = array();
$i = 0;
$jourOuvre = 5;
foreach($data['ferie'] as $row){
$ferie = array($row->start);
while($i < $jourOuvre){
$date_tmp = date("Y-m-d", strtotime($i . 'weekdays', $timestamp));
if (in_array($date_tmp , $ferie)){
$jourOuvre++;
$date_tmp = date("Y-m-d", strtotime('+1days', $date_tmp));
}
$days[] = $date_tmp;
$i++;
}
}
var_dump($days);
this code makes :
699:
array (size=5)
0 => string '2020-05-18' (length=10)
1 => string '2020-05-19' (length=10)
2 => string '2020-05-20' (length=10)
3 => string '2020-05-21' (length=10)
4 => string '2020-05-22' (length=10)
while 2020-05-21 is a holiday.
NB: $data['ferie'] is an array of holidays.
Thanks, guys. I did it.
$data['ferie'] = $this->CalendrierModel->find('ferie');
$timestamp = strtotime ('2020-05-18');
$days = array();
$ferie = array();
$i = 0;
$jourOuvre = 5;
foreach($data['ferie'] as $row){
$ferie[] = $row->start;
}
while($i < $jourOuvre){
$date_tmp = date("Y-m-d", strtotime($i . ' weekdays' , $timestamp));
if(in_array($date_tmp , $ferie)) {
$jourOuvre++;
} else {
$days[] = $date_tmp;
}
$i++;
}
var_dump($days);
Holidays are out of my array :
C:\wamp\www\HELPJUR\application\controllers\Ticket.php:816:
array (size=5)
0 => string '2020-05-18' (length=10)
1 => string '2020-05-19' (length=10)
2 => string '2020-05-20' (length=10)
3 => string '2020-05-22' (length=10)
4 => string '2020-05-25' (length=10)

How to get all months between two dates in PHP?

I have 2 dates. I want to get all months with total days in each.
How can I do this in PHP?
For example
$date1 = '2013-11-13'; // yy-mm-dd format
$date2 = '2014-02-14';
Output
Months Total Days
-----------------------
11-2013 30
12-2013 31
01-2014 31
02-2014 28
Just try with:
$date1 = '2013-11-15';
$date2 = '2014-02-15';
$output = [];
$time = strtotime($date1);
$last = date('m-Y', strtotime($date2));
do {
$month = date('m-Y', $time);
$total = date('t', $time);
$output[] = [
'month' => $month,
'total' => $total,
];
$time = strtotime('+1 month', $time);
} while ($month != $last);
var_dump($output);
Output:
array (size=4)
0 =>
array (size=2)
'month' => string '11-2013' (length=7)
'total' => string '30' (length=2)
1 =>
array (size=2)
'month' => string '12-2013' (length=7)
'total' => string '31' (length=2)
2 =>
array (size=2)
'month' => string '01-2014' (length=7)
'total' => string '31' (length=2)
3 =>
array (size=2)
'month' => string '02-2014' (length=7)
'total' => string '28' (length=2)
Try the below given code :
$date1 = '2013-11-15'; // yy-mm-dd format
$date2 = '2014-02-15';
$start = new DateTime($date1);
$start->modify('first day of this month');
$end = new DateTime($date2);
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") ." " ;
echo cal_days_in_month(CAL_GREGORIAN,$dt->format("m"),$dt->format("Y")) . "<br/>";
}
Also, checkout this link for more
i used timestamp and date:
$date1 = '2013-11-15'; // yy-mm-dd format
$date2 = '2014-02-15';
$d1 = strtotime('2013-11-15');
$d2 = strtotime('2014-02-15');
while ($d1 <= $d2) {
echo date('m-d-Y', $d1)." | ";
echo cal_days_in_month(CAL_GREGORIAN, date('m', $d1), date('Y', $d1)) ."<br>";
$d1 = strtotime("+1 month", $d1);
}
This should help you, Check out,
$date1 = new DateTime('2013-11-15');
$date1->modify('first day of this month');
$date2 = new DateTime('2014-02-15');
$date2->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$value = new DatePeriod($date1, $interval, $date2);
foreach ($value as $dates) {
echo $dates->format("m- Y")."-->".cal_days_in_month(0,$dates->format("m"),$dates->format("Y"))."<br>\n";
}
This is what i came up with:
$arr_months = array();
$date1 = new DateTime('2013-11-15');
$date2 = new DateTime('2014-02-15');
$month1 = new DateTime($date1->format('Y-m')); //The first day of the month of date 1
while ($month1 < $date2) { //Check if the first day of the next month is still before date 2
$arr_months[$month1->format('Y-m')] = cal_days_in_month(CAL_GREGORIAN, $month1->format('m'), $month1->format('Y')); //Add it to the array
$month1->modify('+1 month'); //Add one month and repeat
}
print_r($arr_months);
It creates an associative array with the month as key and the number of days as value.
The array created from the example would be:
Array
(
[2013-11] => 30
[2013-12] => 31
[2014-01] => 31
[2014-02] => 28
)
With a foreach loop you will be able to scroll trough the array easily.
You can use the DateTime class along with cal_day_in_month() like this
$datetime1 = "2014-02-15";
$datetime2= "2013-03-15";
$date1 = new DateTime($datetime1);
$date2 = new DateTime($datetime2);
while (date_format($date2, 'Y-m') <= date_format($date1, 'Y-m'))
{
$date2 = $date2->add(new DateInterval('P1M'));
echo $date2->format('Y-m')." | ".cal_days_in_month(CAL_GREGORIAN, $date2->format('m'), $date2->format('Y'))."<br>";
}

How to decrement months on a php datetime object until it matches the month of a second date time object?

I have two objects $o_NewRegDate (registration date) and $o_NewNowDate(current date).
If we dump the year and month for $o_NewRegDate:
var_dump (date_format($o_NewRegDate, 'Y-m'));
string '2009-09' (length=7)
If we dump the year and month for $o_NewNowDate:
var_dump (date_format($o_NewNowDate, 'Y-m'));
string '2013-09' (length=7)
What is the best way to decrement the months in $o_NewNowDate (storing each iteration in an array) until we reach the year and month of $o_NewRegDate?
Desired Output array would be something like this:
array (size=61)
'2013-09-01' => string '09-2013' (length=7)
'2013-08-01' => string '08-2013' (length=7)
'2013-07-01' => string '07-2013' (length=7)
(...)
'2010-01-01' => string '01-2010' (length=7)
'2009-12-01' => string '12-2009' (length=7)
'2009-11-01' => string '11-2009' (length=7)
'2009-10-01' => string '10-2009' (length=7)
'2009-09-01' => string '09-2009' (length=7)
$o_NewRegDate = '2009-09-01'; $o_NewNowDate = '2013-09-01';
$iDateStart = strtotime( $o_NewRegDate );
$iDateEnd = strtotime( $o_NewNowDate );
$iEnd = ( $iDateEnd - $iDateStart ) / 2678400; //60*60*24*31
$aDate = array();
for( $i = 0; $i <= $iEnd; $i++ ) {
$aDate[] = date('Y-m-d', strtotime('+'. $i .' month', $iDateStart));
}
$aDate = array_reverse( $aDate );
improve by yourself especially $iEnd variable.
$o_NewRegDate = '2009-09-01';
$o_NewNowDate = '2013-09-18';
$startDate = new \DateTime($o_NewNowDate);
echo 'Start date: ', $startDate->format('Y-m-d') , PHP_EOL;
$endDate = new \DateTime($o_NewRegDate);
echo 'End date: ', $endDate->format('Y-m-d') , PHP_EOL;
$interval = new \DateInterval('P1M');
$monthPeriod = new \DatePeriod ($endDate, $interval, $startDate);
foreach ($monthPeriod as $key => $monthDate) {
echo $monthDate->format('Y-m') , PHP_EOL;
}
use this simple code.
$date2='2014-07-12';
$monthArr=explode('-',$date2);
$numDays=cal_days_in_month(CAL_GREGORIAN, $monthArr[1], $monthArr[2]);
$tempDate=date('Y-m-d',strtotime($date2));
$regDate=date('Y-m-d',strtotime("-$numDays days",strtotime($tempDate)));

Grouping dates into Months in php

Created the make time below to display biweekly dates
<?php
$date1 = "07/05/2013";
$date2 = date('M j, Y', strtotime($date1 . " + 14 day"));
$date3 = date('M j, Y', strtotime($date2 . " + 14 day"));
$date4 = date('M j, Y', strtotime($date3 . " + 14 day"));
$date5 = date('M j, Y', strtotime($date4 . " + 14 day"));
$date6 = date('M j, Y', strtotime($date5 . " + 14 day"));
$date7 = date('M j, Y', strtotime($date6 . " + 14 day"));
$date8 = date('M j, Y', strtotime($date7 . " + 14 day"));
$date9 = date('M j, Y', strtotime($date8 . " + 14 day"));
$date10 = date('M j, Y', strtotime($date9 . " + 14 day"));
$date11 = date('M j, Y', strtotime($date10 . " + 14 day"));
$date12 = date('M j, Y', strtotime($date11 . " + 14 day"));
$date13 = date('M j, Y', strtotime($date12 . " + 14 day"));
$date14 = date('M j, Y', strtotime($date13 . " + 14 day"));
$date15 = date('M j, Y', strtotime($date14 . " + 14 day"));
$date16 = date('M j, Y', strtotime($date15 . " + 14 day"));
$date17 = date('M j, Y', strtotime($date16 . " + 14 day"));
$date18 = date('M j, Y', strtotime($date17 . " + 14 day"));
?>
How can I get it to group together by Month? Let say I want to know how many dates land in August, or December. Also if I wanted to get how many dates till end of the year? Helping hand will be greatly appreciated.
I wasn't sure exactly what you meant about grouping by months
but this will use use an array to declare all the dates and you can change the $limit variable to increase or decrease the amount of dates.
change $dates to adjust your original date
$left is how many days are left in the array (it triggers after the first day is hit, can be changed to trigger on the last day)
and $amount is how many dates are in august
<?php
$limit = 17;
$dates = array("07-05-2013");
for ($i=1; $i<=$limit; $i++){
$dates[$i]=date('d-m-Y', strtotime($dates[$i-1] . "+ 14 days"));
}
foreach (array_keys($dates) as $key){
$value = date('m', strtotime($dates[$key]));
if ($value == "08"){
$amount = $amount + 1;
}
if ($amount == 1){
$left = $limit-$key;
}
}
print_r ($dates);
echo "<br>";
echo $amount . "<br>" . $left;
?>
Try this:
<?php
// initiate months
$month_arr = Array();
for ($i=1; $i<=12; $i++){
// no. of dates
$month_arr[$i] = 0;
}
$date_arr = Array();
$date_start = "07/05/2013";
$date_arr[] = date('M j, Y', strtotime($date_start));
for ($i=1; $i<=17; $i++){
$date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
$month = date('n', strtotime($date_temp));
$month_arr[$month] += 1;
$date_arr[] = $date_temp;
}
foreach ($month_arr as $k => $v){
echo "<BR>Month: " . $k . ", No. of dates: " . $v;
}
// all dates
echo "<BR>All dates<BR>";
var_dump ($date_arr);
?>
You can extend this logic to group the actual dates by months rather than getting only the count of dates in a month. This is the solution for that:
<?php
// initiate months
$month_arr = Array(
'January' => Array('num_dates'=>0, 'dates'=>Array()) ,
'February' => Array('num_dates'=>0, 'dates'=>Array()),
'March' => Array('num_dates'=>0, 'dates'=>Array()),
'April' => Array('num_dates'=>0, 'dates'=>Array()),
'May' => Array('num_dates'=>0, 'dates'=>Array()),
'June' => Array('num_dates'=>0, 'dates'=>Array()),
'July' => Array('num_dates'=>0, 'dates'=>Array()),
'August' => Array('num_dates'=>0, 'dates'=>Array()),
'September' => Array('num_dates'=>0, 'dates'=>Array()),
'October' => Array('num_dates'=>0, 'dates'=>Array()),
'November' => Array('num_dates'=>0, 'dates'=>Array()),
'December' => Array('num_dates'=>0, 'dates'=>Array())
);
$date_arr = Array();
$date_start = "07/05/2013";
$date_arr[] = date('M j, Y', strtotime($date_start));
for ($i=1; $i<=17; $i++){
$date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
$month = date('F', strtotime($date_temp));
$month_arr[$month]['dates'][] = $date_temp;
$month_arr[$month]['num_dates'] += 1;
$date_arr[] = $date_temp;
}
foreach ($month_arr as $k => $v){
if (!empty($v)){
if ($v['num_dates'] != 0){
echo "<BR><BR>Month: " . $k;
echo "<BR>No. of dates: " . $v['num_dates'];
foreach ($v['dates'] as $k1=>$v1){
echo "<BR>" . $v1;
}
}
}
}
?>
At this point, $month_arr should have everything you need.
just add an if statement and increment through all 12 months ... if the months are the same
$n=1;
$month=array();//dumpyour dates in an array
$date=array()
while($k<30){
if (date('n', strtotime($date[$k]))==date('n', strtotime($date[$k-1])){
echo $date[$k++];
$m = date('n', strtotime($date[$k]));
$month[$m]=++$n;
}
else{
.....
}
}
As I keep re-iterating on SO, PHP's DateTime classes make most datetime operations trivial.
Your question is not 100% clear, but from your comments it seems you want an array of timestamps, 14 days apart sectioned by month. I don't know if you considered that the months may cross year bondaries, but I took that into consideration in my answer.
The code below should do what you want:-
$start = \DateTime::createFromFormat('d/m/Y', '07/05/2013');
$interval = new \DateInterval('P14D');
$periods = new \DatePeriod($start, $interval, 26);
$dates = array();
foreach($periods as $day){
/** #var \DateTime $day */
$dates[$day->format('Y')][$day->format('M')][] = $day->getTimestamp();
}
var_dump($dates);
Output:-
array (size=2)
2013 =>
array (size=8)
'May' =>
array (size=2)
0 => int 1367936286
1 => int 1369145886
'Jun' =>
array (size=2)
0 => int 1370355486
1 => int 1371565086
'Jul' =>
array (size=3)
0 => int 1372774686
1 => int 1373984286
2 => int 1375193886
'Aug' =>
array (size=2)
0 => int 1376403486
1 => int 1377613086
'Sep' =>
array (size=2)
0 => int 1378822686
1 => int 1380032286
'Oct' =>
array (size=2)
0 => int 1381241886
1 => int 1382451486
'Nov' =>
array (size=2)
0 => int 1383664686
1 => int 1384874286
'Dec' =>
array (size=3)
0 => int 1386083886
1 => int 1387293486
2 => int 1388503086
2014 =>
array (size=5)
'Jan' =>
array (size=2)
0 => int 1389712686
1 => int 1390922286
'Feb' =>
array (size=2)
0 => int 1392131886
1 => int 1393341486
'Mar' =>
array (size=2)
0 => int 1394551086
1 => int 1395760686
'Apr' =>
array (size=2)
0 => int 1396966686
1 => int 1398176286
'May' =>
array (size=1)
0 => int 1399385886
I would recommend that, instead of an array of timestamps, you have an array of DateTime objects instead, then you can manipulate each one as you wish. In which case, the code would look like this:-
$start = \DateTime::createFromFormat('d/m/Y', '07/05/2013');
$interval = new \DateInterval('P14D');
$periods = new \DatePeriod($start, $interval, 26);
$dates = array();
foreach($periods as $day){
/** #var \DateTime $day */
$dates[$day->format('Y')][$day->format('M')][] = $day;
}
$months=array("1","2","3","4","5","6","7","8","9","10","11","12");
$years=array('2010','20111','2012','2013','2014','2015','2016','2017','2018');
$dates = array('2015-04-24','2015-04-28','2017-03-24', '2017-03-24', '2017-04-07', '2017-04-14', '2017-04-21', '2017-04-28');
$result=array();
foreach ($months as $mn) {
foreach ($years as $year) {
$r=array();
foreach ($dates as $month) {
if(date_parse_from_format("Y.n.j", $year)
["year"]==date_parse_from_format("Y.n.j", $month)["year"] &&
date_parse_from_format("Y.n.j", $month)["month"]==$mn){$r[]=$month;}
;}
if(empty($r)){;}else{$result[]=$r;};
;}
;};
var_dump($result);
this code should work, and it will segment according to year and month, please add more years to the $years variable as needed and your dates to the $dates array, note that duplicate values will be eliminated and if you prefer to keep duplicates just add a random letter to the end of each date when you insert it into an array as a key and then delete the extra letter when processing is finished.

pChart, how to display week start or end date with month like "12/2-8" on x-axis?

Right now my pchart week graph display like this way,
But I want to
display week on x-axis 12/2-8, 12/9-15 ('month' followed by '/' followed by 'start date', followed by '-', then 'end date')"
You generate the legend you can have
$weekLegend = getWeekLegend();
var_dump($weekLegend); // for debug purpose
Output
array (size=8)
0 => string '12/09-15' (length=8)
1 => string '12/16-22' (length=8)
2 => string '12/23-29' (length=8)
3 => string '12/30-05' (length=8)
4 => string '01/06-12' (length=8)
5 => string '01/13-19' (length=8)
6 => string '01/20-26' (length=8)
7 => string '01/27-02' (length=8)
Adding it to your dataset defination
// Dataset definition
$DataSet = new pData;
$DataSet->AddPoint($data,"Serie1");
$DataSet->AddPoint(getWeekLegend(),"Serie2");
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie("Serie2"); //<------ set pChat to use legend
Function Used
function getWeekLegend($week = 8) {
$start = new DateTime();
$start->modify("last sunday");
while ( $week > 0 ) {
$date = $start->format("m");
$date .= "/";
$date .= $start->format("d");
$date .= "-";
$start->modify("+6 day");
$date .= $start->format("d");
$start->modify("+1 day");
$weekLegend[] = $date;
$week --;
}
return $weekLegend ;
}

Categories