I'm doing a project for school, where I'm supposed to print a string of dates like this:
Week 36: 4.9.2013 - 10.9.2013
Week 37: 11.9.2013 - 17.9.2013 and so on
EDIT: Removed the old code to save space
This works great with weeks but now there's supposed to be separate days as well printed like this:
Week 36: 4.9.2013 - 10.9.2013
Week 37: 11.9.2013 - 17.9.2013
18.9.2013
191.10.2013
Is there a way I can check if the string has a whole week without an endless if-else scenario?
EDIT
I've edited my code a bit:
$dates = "04.09.2013,05.09.2013,24.12.2015,07.09.2013,08.09.2013,09.09.2013,10.09.2013,11.09.2013,12.09.2013,13.09.2013,14.09.2013,15.09.2013,16.09.2013,17.09.2013,10.01.2013,19.09.2013,20.01.2113,21.09.2013,22.09.2013,23.09.2013,24.09.2013,25.09.2013,26.09.2013,27.09.2013,28.09.2013,29.09.2011,30.09.2013,01.10.2013";
fixDates($dates);
function fixDates($dates) {
$datesArray = explode(',', $dates);
$week_dates = array();
$weeks = array();
$date = array();
$temp_array = array();
$pvmArray[] = array();
$i = 0;
foreach($datesArray as $date) {
$date = explode(".", $date);
$date = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
$week = (int)date('W', $date);
$year = (int)date('Y', $date);
$day = (int)date('w', $date);
$date = date('d.m.Y', $date);
$pvmArray[$i]['week'] = $week;
$pvmArray[$i]['date'] = $date;
$pvmArray[$i]['year'] = $year;
$pvmArray[$i]['day'] = $day;
$i++;
}
foreach ($pvmArray as &$v) {
if (!isset($temp_array[$v["week"]]))
$temp_array[$v["week"]] =& $v;
}
$weeks = array_values($temp_array);
foreach ($weeks as $pvm) {
$week = $pvm["week"];
$year = $pvm["year"];
findWeekDays($week, $pvmArray);
}
}
function findWeekDays($week, $pvmArray) {
echo "<br><b>Viikko " . $week. ": </b><br>";
foreach ($pvmArray as $pvm) {
$weeks = $pvm["week"];
$year = $pvm["year"];
$date = $pvm["date"];
$day = $pvm["day"];
if ($weeks == $week) {
echo $date. ", ";
}
}
}
Now I can get an output like this:
Week 36: 04.09.2013, 05.09.2013, 07.09.2013, 08.09.2013,
Week 52: 24.12.2015,
Week 37: 09.09.2013, 10.09.2013, 11.09.2013, 12.09.2013, 13.09.2013, 14.09.2013, 15.09.2013,
Week 38: 16.09.2013, 17.09.2013, 19.09.2013, 21.09.2013, 22.09.2013,
Week 2: 10.01.2013,
Week 3: 20.01.2113,
My problem now is that I still have to group the days that are in the same week, like this
Week 37: 09.09.2013 - 15.09.2013
Any ideas on this one? I know that I can first get the year, then week and then the day of the date (make a new array for this) and then compare the day to the previous one, but that just seems pretty hard and not very logical. Thanks for the comments, it really gave me new ideas to think about.
Enjoy ! This is begin rest you have to do self.
$weeks = array();
foreach($dates as $date)
$weeks[date('W', strtotime($date))][] = $date;
foreach ($weeks as $week => $days)
{
if (! sizeof($days))
continue;
echo 'Week '.$week.': '.implode(', ',$days)."
";
}