Suggesties
Hallo php'ers,
Ik ben voor een vriend een PHP kalender aan het maken waarin je afspraken e.d. kan toevoegen, maar ik vind vrij omslachtig hoe ik het doe. Of ik doe wat fout of niet, maar zou iemand kunnen zeggen hoe het makkelijker kan? Hiermee bouw ik de kalender datums array op.
Opmerking, het werkt allemaal wel het gaat meer om de manier waarop waar ik me zorgen om maak. (Ik ben ook nog een beginnende phper dus laat de beginners foutjes maar achterwege :p)
Ik ben voor een vriend een PHP kalender aan het maken waarin je afspraken e.d. kan toevoegen, maar ik vind vrij omslachtig hoe ik het doe. Of ik doe wat fout of niet, maar zou iemand kunnen zeggen hoe het makkelijker kan? Hiermee bouw ik de kalender datums array op.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
$caldays = array();
$calweeks = array();
$calcontent = array();
$calmonth = 0;
$calyear = 0;
$numdays = 0;
if($_GET['action'] == 'cal' || empty($_GET))
{
// Confirm date -> Valid date || Current date
if(confirmDate($_GET['month'], $_GET['year']))
{
$calmonth = $_GET['month'];
$calyear = $_GET['year'];
}
else
{
$calmonth = date('n', time());
$calyear = date('Y', time());
}
// Set caldays
for($i = 1; getNumDays($calmonth, $calyear) >= $i; $i++)
{
$caldays[] = $i;
}
// Set calweeks
// For every day put the week num in array if not exists
foreach($caldays as $day)
{
if(!in_array(date('W', strtotime($day.'-'.$calmonth.'-'.$calyear)), $calweeks))
{
$calweeks[] = intval(date('W', strtotime($day.'-'.$calmonth.'-'.$calyear)));
}
}
// Add content to array
// For every week the days of that week in array
foreach($calweeks as $key => $week)
{
// For first week add last days of previous month if needed
if($key == 0)
{
$useweek = $week;
}
elseif($key == 1)
{
$cnt = 7 - count($tmpadd);
for($i = 0; $cnt > $i; $i++)
{
// Set needed vals
$newday = getNumDays($calmonth - 1, ($calmonth == 1 ? $calyear - 1 : $calyear)) - $i;
array_unshift($calcontent[$useweek], array($newday => 'off'));
}
}
// Tmp array to add
$tmpadd = array();
foreach($caldays as $day)
{
if(intval(date('W', strtotime($day.'-'.$calmonth.'-'.$calyear))) == $week)
{
// For the last day of last week add rest of days if needed
if($day == getNumDays($calmonth, $calyear))
{
$tmpadd[] = (date('d-m-Y') == date('d-m-Y', strtotime($day.'-'.$calmonth.'-'.$calyear)) ? array($day => 'selected') : array($day => 'on'));
$cnt = 7 - count($tmpadd);
for($i = 1; $cnt >= $i; $i++)
{
$tmpadd[] = array($i => 'off');
}
}
else
{
$tmpadd[] = (date('d-m-Y') == date('d-m-Y', strtotime($day.'-'.$calmonth.'-'.$calyear)) ? array($day => 'selected') : array($day => 'on'));
}
}
}
$calcontent[$week] = $tmpadd;
}
?>
$caldays = array();
$calweeks = array();
$calcontent = array();
$calmonth = 0;
$calyear = 0;
$numdays = 0;
if($_GET['action'] == 'cal' || empty($_GET))
{
// Confirm date -> Valid date || Current date
if(confirmDate($_GET['month'], $_GET['year']))
{
$calmonth = $_GET['month'];
$calyear = $_GET['year'];
}
else
{
$calmonth = date('n', time());
$calyear = date('Y', time());
}
// Set caldays
for($i = 1; getNumDays($calmonth, $calyear) >= $i; $i++)
{
$caldays[] = $i;
}
// Set calweeks
// For every day put the week num in array if not exists
foreach($caldays as $day)
{
if(!in_array(date('W', strtotime($day.'-'.$calmonth.'-'.$calyear)), $calweeks))
{
$calweeks[] = intval(date('W', strtotime($day.'-'.$calmonth.'-'.$calyear)));
}
}
// Add content to array
// For every week the days of that week in array
foreach($calweeks as $key => $week)
{
// For first week add last days of previous month if needed
if($key == 0)
{
$useweek = $week;
}
elseif($key == 1)
{
$cnt = 7 - count($tmpadd);
for($i = 0; $cnt > $i; $i++)
{
// Set needed vals
$newday = getNumDays($calmonth - 1, ($calmonth == 1 ? $calyear - 1 : $calyear)) - $i;
array_unshift($calcontent[$useweek], array($newday => 'off'));
}
}
// Tmp array to add
$tmpadd = array();
foreach($caldays as $day)
{
if(intval(date('W', strtotime($day.'-'.$calmonth.'-'.$calyear))) == $week)
{
// For the last day of last week add rest of days if needed
if($day == getNumDays($calmonth, $calyear))
{
$tmpadd[] = (date('d-m-Y') == date('d-m-Y', strtotime($day.'-'.$calmonth.'-'.$calyear)) ? array($day => 'selected') : array($day => 'on'));
$cnt = 7 - count($tmpadd);
for($i = 1; $cnt >= $i; $i++)
{
$tmpadd[] = array($i => 'off');
}
}
else
{
$tmpadd[] = (date('d-m-Y') == date('d-m-Y', strtotime($day.'-'.$calmonth.'-'.$calyear)) ? array($day => 'selected') : array($day => 'on'));
}
}
}
$calcontent[$week] = $tmpadd;
}
?>
Opmerking, het werkt allemaal wel het gaat meer om de manier waarop waar ik me zorgen om maak. (Ik ben ook nog een beginnende phper dus laat de beginners foutjes maar achterwege :p)
Gewijzigd op 28/03/2014 17:50:26 door Victor G
Er zijn nog geen reacties op dit bericht.




