1<?php
2
3/*
4date validation and parsing functions
5
6These functions refer to the session variable defining the date format
7The date format is defined in SystemParameters called DefaultDateFormat
8this can be a string either 'd/m/Y' for UK/Australia/New Zealand dates or
9'm/d/Y' for US/Canada format dates
10
11or Y/m/d  for Sweden ;) Anders Eriksson anders@weberp.se.
12
13or d.m.Y  for Germany ;) Juergen Ruemmler heinrich@ruemmler.net
14
15*/
16
17function Is_date($DateEntry) {
18
19	$DateEntry =Trim($DateEntry);
20
21	//echo '<BR>The date entered is ' . $DateEntry;
22
23	if (mb_strpos($DateEntry,'/')) {
24		$Date_Array = explode('/',$DateEntry);
25	} elseif (mb_strpos ($DateEntry,'-')) {
26		$Date_Array = explode('-',$DateEntry);
27	} elseif (mb_strpos ($DateEntry,'.')) {
28		$Date_Array = explode('.',$DateEntry);
29	} elseif (mb_strlen($DateEntry)==6) {
30		$Date_Array[0]= mb_substr($DateEntry,0,2);
31		$Date_Array[1]= mb_substr($DateEntry,2,2);
32		$Date_Array[2]= mb_substr($DateEntry,4,2);
33	} elseif (mb_strlen($DateEntry)==8) {
34		$Date_Array[0]= mb_substr($DateEntry,0,2);
35		$Date_Array[1]= mb_substr($DateEntry,2,2);
36		$Date_Array[2]= mb_substr($DateEntry,4,4);
37	}
38
39	if (!isset($Date_Array) or sizeof($Date_Array)<3) {
40		return 0;
41	}
42
43	if ((int)$Date_Array[2] >9999) {
44		return 0;
45	}
46
47
48	if (is_long((int)$Date_Array[0]) AND is_long((int)$Date_Array[1]) AND is_long((int)$Date_Array[2])) {
49
50		if (($_SESSION['DefaultDateFormat']=='d/m/Y') OR ($_SESSION['DefaultDateFormat']=='d.m.Y')){
51
52			if (checkdate((int)$Date_Array[1],(int)$Date_Array[0],(int)$Date_Array[2])){
53				return 1;
54			} else {
55				return 0;
56			}
57		} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
58
59			if (checkdate((int)$Date_Array[0],(int)$Date_Array[1],(int)$Date_Array[2])){
60				return 1;
61			} else {
62				return 0;
63			}
64		} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d'){
65
66			if (checkdate((int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0])){
67				return 1;
68			} else {
69				return 0;
70			}
71		} elseif ($_SESSION['DefaultDateFormat']=='Y-m-d'){
72			if (checkdate((int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0])){
73				return 1;
74			} else {
75				return 0;
76			}
77		} else { /*Can't be in an appropriate DefaultDateFormat */
78			return 0;
79		}
80	}
81} //end of Is_Date function
82
83
84function MonthAndYearFromSQLDate($DateEntry, $useShortMonthAndYear = false) {
85
86	if (mb_strpos($DateEntry,'/')) {
87		$Date_Array = explode('/',$DateEntry);
88	} elseif (mb_strpos ($DateEntry,'-')) {
89		$Date_Array = explode('-',$DateEntry);
90	} elseif (mb_strpos ($DateEntry,'.')) {
91		$Date_Array = explode('.',$DateEntry);
92	}
93
94	if (mb_strlen($Date_Array[2])>4) {
95		$Date_Array[2]= mb_substr($Date_Array[2],0,2);
96	}
97
98	$MonthAndYear = '';
99	$timestamp = mktime(0,0,0, (int)$Date_Array[1], (int)$Date_Array[2], (int)$Date_Array[0]);
100
101	if ($useShortMonthAndYear) {
102		// 2-digit month and year: 04/20.
103		// Useful for Graphs with many plot references.
104		$MonthAndYear = date('m/y', $timestamp);
105	} else {
106		$MonthName = GetMonthText(date('n', $timestamp));
107		$MonthAndYear = $MonthName . ' ' . date('Y', $timestamp);
108	}
109
110	return $MonthAndYear;
111}
112
113function GetMonthText($MonthNumber){
114	switch ($MonthNumber) {
115		case 1:
116			$Month = _('January');
117			break;
118		case 2:
119			$Month = _('February');
120			break;
121		case 3:
122			$Month = _('March');
123			break;
124		case 4:
125			$Month = _('April');
126			break;
127		case 5:
128			$Month = _('May');
129			break;
130		case 6:
131			$Month = _('June');
132			break;
133		case 7:
134			$Month = _('July');
135			break;
136		case 8:
137			$Month = _('August');
138			break;
139		case 9:
140			$Month = _('September');
141			break;
142		case 10:
143			$Month = _('October');
144			break;
145		case 11:
146			$Month = _('November');
147			break;
148		case 12:
149			$Month = _('December');
150			break;
151		default:
152			$Month = _('error');
153			break;
154	}
155	return $Month;
156}
157
158function GetWeekDayText ($WeekDayNumber){
159	$Day='';
160	switch ($WeekDayNumber) {
161		case 0:
162			$Day = _('Sunday');
163			break;
164		case 1:
165			$Day = _('Monday');
166			break;
167		case 2:
168			$Day = _('Tuesday');
169			break;
170		case 3:
171			$Day = _('Wednesday');
172			break;
173		case 4:
174			$Day = _('Thursday');
175			break;
176		case 5:
177			$Day = _('Friday');
178			break;
179		case 6:
180			$Day = _('Saturday');
181			break;
182	}
183	return $Day;
184}
185
186function DisplayDateTime() {
187	// Long date and time in locale format.
188	// Could be replace by IntlDateFormatter (available on PHP 5.3.0 or later). See http://php.net/manual/en/class.intldateformatter.php
189	switch ($_SESSION['Language']) {
190		case 'en_GB.utf8':
191			$long_datetime = GetWeekDayText(date('w')) . ' ' . date('j') . ' ' . GetMonthText(date('n')) . ' ' . date('Y') . ' ' . date('G:i');
192			break;
193		case 'en_US.utf8':
194			$long_datetime = GetWeekDayText(date('w')) . ', ' . GetMonthText(date('n')) . ' ' . date('j') . ', '. date('Y') . ' ' . date('G:i');
195			break;
196		case 'es_ES.utf8':
197			$long_datetime = GetWeekDayText(date('w')) . ' ' . date('j') . ' de ' . GetMonthText(date('n')) . ' de ' . date('Y') . ' ' . date('G:i');
198			break;
199		case 'fr_FR.utf8':
200			$long_datetime = GetWeekDayText(date('w')) . ' ' . date('j') . ' ' . GetMonthText(date('n')) . ' ' . date('Y') . ' ' . date('G:i');
201			break;
202		default:
203			$long_datetime = GetWeekDayText(date('w')) . ' ' . date('j') . ' ' . GetMonthText(date('n')) . ' ' . date('Y') . ' ' . date('G:i');
204			break;
205	}
206	return $long_datetime;
207/*	return GetWeekDayText(date('w')) . ' ' . date('j') . ' ' . GetMonthText(date('n')) . ' ' . date('G:i') ;*/
208}
209
210function DayOfWeekFromSQLDate($DateEntry) {
211
212	if (mb_strpos($DateEntry,'/')) {
213		$Date_Array = explode('/',$DateEntry);
214	} elseif (mb_strpos ($DateEntry,'-')) {
215		$Date_Array = explode('-',$DateEntry);
216	} elseif (mb_strpos ($DateEntry,'.')) {
217		$Date_Array = explode('.',$DateEntry);
218	}
219
220	if (mb_strlen($Date_Array[2])>4) {
221		$Date_Array[2]= mb_substr($Date_Array[2],0,2);
222	}
223
224	return date( 'w', mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0]));
225
226}
227
228
229function DayOfMonthFromSQLDate($DateEntry) {
230
231	if (mb_strpos($DateEntry,'/')) {
232		$Date_Array = explode('/',$DateEntry);
233	} elseif (mb_strpos ($DateEntry,'-')) {
234		$Date_Array = explode('-',$DateEntry);
235	} elseif (mb_strpos ($DateEntry,'.')) {
236		$Date_Array = explode('.',$DateEntry);
237	}
238
239	if (mb_strlen($Date_Array[2])>4) {
240		$Date_Array[2]= mb_substr($Date_Array[2],0,2);
241	}
242
243	return date( 'j', mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0]));
244
245}
246
247
248// Returns the timestamp for the financial year end. To find other year ends,
249// use $YearIncrement to move back and forward. in -1 gives last year end, 1
250// gives next year end.
251
252function YearEndDate($MonthNo, $YearIncrement) {
253	if (Date('m') > $MonthNo) {
254		$Year = Date('Y')+1+$YearIncrement;
255	} else {
256		$Year = Date('Y')+$YearIncrement;
257	}
258	return mktime(0,0,0,$MonthNo+1,0,$Year);
259}
260
261function ConvertSQLDate($DateEntry) {
262
263//for MySQL dates are in the format YYYY-mm-dd
264
265
266	if (mb_strpos($DateEntry,'/')) {
267		$Date_Array = explode('/',$DateEntry);
268	} elseif (mb_strpos ($DateEntry,'-')) {
269		$Date_Array = explode('-',$DateEntry);
270	} elseif (mb_strpos ($DateEntry,'.')) {
271		$Date_Array = explode('.',$DateEntry);
272	} else {
273		prnMsg(_('The date does not appear to be in a valid format. The date being converted from SQL format was:') . ' ' . $DateEntry,'error');
274		switch ($_SESSION['DefaultDateFormat']) {
275			case 'd/m/Y':
276				return '0/0/000';
277				break;
278			case 'd.m.Y':
279				return '0.0.000';
280				break;
281			case 'm/d/Y':
282				return '0/0/0000';
283				break;
284			case 'Y/m/d':
285				return '0000/0/0';
286				break;
287			case 'Y-m-d':
288				return '0000-0-0';
289				break;
290		}
291	}
292
293	if (mb_strlen($Date_Array[2])>4) {  /*chop off the time stuff */
294		$Date_Array[2]= mb_substr($Date_Array[2],0,2);
295	}
296
297	if ($_SESSION['DefaultDateFormat']=='d/m/Y'){
298		return $Date_Array[2].'/'.$Date_Array[1].'/'.$Date_Array[0];
299	} elseif ($_SESSION['DefaultDateFormat']=='d.m.Y'){
300		return $Date_Array[2].'.'.$Date_Array[1].'.'.$Date_Array[0];
301	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
302		return $Date_Array[1].'/'.$Date_Array[2].'/'.$Date_Array[0];
303	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d'){
304		return $Date_Array[0].'/'.$Date_Array[1].'/'.$Date_Array[2];
305	} elseif ($_SESSION['DefaultDateFormat']=='Y-m-d'){
306		return $Date_Array[0].'-'.$Date_Array[1].'-'.$Date_Array[2];
307	}
308} // end function ConvertSQLDate
309
310function ConvertSQLDateTime($DateEntry) {
311
312//for MySQL dates are in the format YYYY-mm-dd H:i:s
313
314
315	if (mb_strpos($DateEntry,'/')) {
316		$Date_Array = explode('/',$DateEntry);
317	} elseif (mb_strpos ($DateEntry,'-')) {
318		$Date_Array = explode('-',$DateEntry);
319	} elseif (mb_strpos ($DateEntry,'.')) {
320		$Date_Array = explode('.',$DateEntry);
321	} else {
322		prnMsg(_('The date does not appear to be in a valid format. The date being converted from SQL format was:') . ' ' . $DateEntry,'error');
323		switch ($_SESSION['DefaultDateFormat']) {
324			case 'd/m/Y':
325				return '0/0/000';
326				break;
327			case 'd.m.Y':
328				return '0.0.000';
329				break;
330			case 'm/d/Y':
331				return '0/0/0000';
332				break;
333			case 'Y/m/d':
334				return '0000/0/0';
335				break;
336		}
337	}
338
339	if (mb_strlen($Date_Array[2])>4) {
340		$Time = mb_substr($Date_Array[2],3,8);
341		$Date_Array[2]= mb_substr($Date_Array[2],0,2);
342	}
343
344	if ($_SESSION['DefaultDateFormat']=='d/m/Y'){
345		return $Date_Array[2].'/'.$Date_Array[1].'/'.$Date_Array[0] . ' ' . $Time;
346	} elseif ($_SESSION['DefaultDateFormat']=='d.m.Y'){
347		return $Date_Array[2].'.'.$Date_Array[1].'.'.$Date_Array[0] . ' ' . $Time;
348	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
349		return $Date_Array[1].'/'.$Date_Array[2].'/'.$Date_Array[0] . ' ' . $Time;
350	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d'){
351		return $Date_Array[0].'/'.$Date_Array[1].'/'.$Date_Array[2] . ' ' . $Time;
352	}
353
354} // end function ConvertSQLDate
355
356function SQLDateToEDI($DateEntry) {
357
358//for MySQL dates are in the format YYYY-mm-dd
359//EDI format 102 dates are in the format CCYYMMDD - just need to lose the seperator
360
361	if (mb_strpos($DateEntry,'/')) {
362		$Date_Array = explode('/',$DateEntry);
363	} elseif (mb_strpos ($DateEntry,'-')) {
364		$Date_Array = explode('-',$DateEntry);
365	} elseif (mb_strpos ($DateEntry,'.')) {
366		$Date_Array = explode('.',$DateEntry);
367	}
368
369	if (mb_strlen($Date_Array[2])>4) {  /*chop off the time stuff */
370		$Date_Array[2]= mb_substr($Date_Array[2],0,2);
371	}
372
373	return $Date_Array[0].$Date_Array[1].$Date_Array[2];
374
375} // end function SQLDateToEDI
376
377function ConvertToEDIDate($DateEntry) {
378
379/* takes a date in a the format specified in $_SESSION['DefaultDateFormat']
380and converts to a yyyymmdd - EANCOM format 102*/
381
382
383	$DateEntry = trim($DateEntry);
384
385	if (mb_strpos($DateEntry,'/')) {
386		$Date_Array = explode('/',$DateEntry);
387	} elseif (mb_strpos ($DateEntry,'-')) {
388		$Date_Array = explode('-',$DateEntry);
389	} elseif (mb_strpos ($DateEntry,'.')) {
390		$Date_Array = explode('.',$DateEntry);
391	} elseif (mb_strlen($DateEntry)==6) {
392		$Date_Array[0]= mb_substr($DateEntry,0,2);
393		$Date_Array[1]= mb_substr($DateEntry,2,2);
394		$Date_Array[2]= mb_substr($DateEntry,4,2);
395	} elseif (mb_strlen($DateEntry)==8) {
396		$Date_Array[0]= mb_substr($DateEntry,0,2);
397		$Date_Array[1]= mb_substr($DateEntry,2,2);
398		$Date_Array[2]= mb_substr($DateEntry,4,4);
399	}
400
401
402//to modify assumption in 2030
403
404	if ((int)$Date_Array[2] <60) {
405		$Date_Array[2] = '20'.$Date_Array[2];
406	} elseif ((int)$Date_Array[2] >59 AND (int)$Date_Array[2] <100) {
407		$Date_Array[2] = '19'.$Date_Array[2];
408	} elseif ((int)$Date_Array[2] >9999) {
409		return 0;
410	}
411
412	if (($_SESSION['DefaultDateFormat']=='d/m/Y') || ($_SESSION['DefaultDateFormat']=='d.m.Y')){
413		return $Date_Array[2].$Date_Array[1].$Date_Array[0];
414
415	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y') {
416		return $Date_Array[2].$Date_Array[0].$Date_Array[1];
417
418	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d') {
419		return $Date_Array[1].$Date_Array[2].$Date_Array[0];
420
421	}
422
423} // end function to convert DefaultDateFormat Date to EDI format 102
424
425function ConvertEDIDate($DateEntry, $EDIFormatCode) {
426
427	/*EDI Format codes:
428		102  -  CCYYMMDD
429		203  -  CCYYMMDDHHMM
430		616  -  CCYYWW  - cant handle the week number
431		718  -  CCYYMMDD-CCYYMMDD  can't handle this either a date range
432	*/
433
434
435	switch ($EDIFormatCode) {
436	case 102:
437		if ($_SESSION['DefaultDateFormat']=='d/m/Y'){
438			return mb_substr($DateEntry,6,2).'/'.mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,0,4);
439
440		} elseif ($_SESSION['DefaultDateFormat']=='d.m.Y') {
441						return mb_substr($DateEntry,6,2).'.'.mb_substr($DateEntry,4,2).'.'.mb_substr($DateEntry,0,4);
442
443		} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y') {
444						return mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,6,2).'/'.mb_substr($DateEntry,0,4);
445
446		} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d') {
447						return mb_substr($DateEntry,0,4).'/'.mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,6,2);
448
449		}
450		break;
451	case 203:
452		if ($_SESSION['DefaultDateFormat']=='d/m/Y') {
453			return mb_substr($DateEntry,6,2).'/'.mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,0,4).' ' . mb_substr($DateEntry,6,2).':' . mb_substr($DateEntry,8,2);
454
455		} elseif ($_SESSION['DefaultDateFormat']=='d.m.Y') {
456						return mb_substr($DateEntry,6,2).'.'.mb_substr($DateEntry,4,2).'.'.mb_substr($DateEntry,0,4).' ' . mb_substr($DateEntry,6,2).':' . mb_substr($DateEntry,8,2);
457
458		} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y') {
459						return mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,6,2).'/'.mb_substr($DateEntry,0,4).' ' . mb_substr($DateEntry,6,2).':' . mb_substr($DateEntry,8,2);
460
461		} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d') {
462						return mb_substr($DateEntry,0,4).'/'.mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,6,2).' ' . mb_substr($DateEntry,6,2).':' . mb_substr($DateEntry,8,2);
463
464		}
465		break;
466	case 616:
467		/*multiply the week number by 7 and add to the 1/1/CCYY */
468		return date($_SESSION['DefaultDateFormat'], mktime(0,0,0, 1,1+(7*(int)mb_substr($DateEntry,4,2)),mb_substr($DateEntry,0,4)));
469		break;
470	case 718:
471		if ($_SESSION['DefaultDateFormat']=='d/m/Y'){
472			return mb_substr($DateEntry,6,2).'/'.mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,0,4) . ' - '. mb_substr($DateEntry,15,2).'/'.mb_substr($DateEntry,13,2).'/'.mb_substr($DateEntry,9,4);
473		} elseif ($_SESSION['DefaultDateFormat']=='d.m.Y') {
474						return mb_substr($DateEntry,6,2).'.'.mb_substr($DateEntry,4,2).'.'.mb_substr($DateEntry,0,4) . ' - '. mb_substr($DateEntry,15,2).'.'.mb_substr($DateEntry,13,2).'.'.mb_substr($DateEntry,9,4);
475		} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y') {
476						return mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,6,2).'/'.mb_substr($DateEntry,0,4).' - '. mb_substr($DateEntry,13,2).'/'.mb_substr($DateEntry,15,2).'/'.mb_substr($DateEntry,9,4);
477		} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d') {
478						return mb_substr($DateEntry,0,4).'/'.mb_substr($DateEntry,4,2).'/'.mb_substr($DateEntry,6,2).' - '. mb_substr($DateEntry,13,2).'/'.mb_substr($DateEntry,15,2).'/'.mb_substr($DateEntry,9,4);
479
480		}
481
482		break;
483	}
484
485
486}
487
488
489
490function Format_Date($DateEntry) {
491
492	$DateEntry =trim($DateEntry);
493
494	if (mb_strpos($DateEntry,'/')) {
495		$Date_Array = explode('/',$DateEntry);
496	} elseif (mb_strpos ($DateEntry,'-')) {
497		$Date_Array = explode('-',$DateEntry);
498	} elseif (mb_strpos ($DateEntry,'.')) {
499		$Date_Array = explode('.',$DateEntry);
500	} elseif (mb_strlen($DateEntry)==6) {
501		$Date_Array[0]= mb_substr($DateEntry,0,2);
502		$Date_Array[1]= mb_substr($DateEntry,2,2);
503		$Date_Array[2]= mb_substr($DateEntry,4,2);
504	} elseif (mb_strlen($DateEntry)==8) {
505		$Date_Array[0]= mb_substr($DateEntry,0,2);
506		$Date_Array[1]= mb_substr($DateEntry,2,2);
507		$Date_Array[2]= mb_substr($DateEntry,4,4);
508	}
509
510//to modify assumption in 2030
511
512	if ((int)$Date_Array[2] <60) {
513		$Date_Array[2] = '20'.$Date_Array[2];
514	} elseif ((int)$Date_Array[2] >59 AND (int)$Date_Array[2] <100){
515		$Date_Array[2] = '19'.$Date_Array[2];
516	} elseif ((int)$Date_Array[2] >9999) {
517		return 0;
518	}
519
520	if (is_long((int)$Date_Array[0]) AND is_long((int)$Date_Array[1]) AND is_long((int)$Date_Array[2])) {
521		if ($_SESSION['DefaultDateFormat']=='d/m/Y'){
522			if (checkdate((int)$Date_Array[1],(int)$Date_Array[0],(int)$Date_Array[2])){
523				return $Date_Array[0].'/'.$Date_Array[1].'/'.$Date_Array[2];
524			}
525		} elseif ($_SESSION['DefaultDateFormat']=='d.m.Y'){
526			if (checkdate((int)$Date_Array[1],(int)$Date_Array[0],(int)$Date_Array[2])){
527				return $Date_Array[0].'.'.$Date_Array[1].'.'.$Date_Array[2];
528			}
529		} elseif ($_SESSION['DefaultDateFormat']='m/d/Y'){
530			if (checkdate((int)$Date_Array[0],(int)$Date_Array[1],(int)$Date_Array[2])){
531				return $Date_Array[0].'/'.$Date_Array[1].'/'.$Date_Array[2];
532			}
533		} elseif ($_SESSION['DefaultDateFormat']='Y/m/d'){
534			if (checkdate((int)$Date_Array[2],(int)$Date_Array[0],(int)$Date_Array[1])){
535				return $Date_Array[0].'/'.$Date_Array[1].'/'.$Date_Array[2];
536			}
537		} elseif ($_SESSION['DefaultDateFormat']='Y-m-d'){
538			if (checkdate((int)$Date_Array[2],(int)$Date_Array[0],(int)$Date_Array[1])){
539				return $Date_Array[0].'-'.$Date_Array[1].'-'.$Date_Array[2];
540			}
541		} // end if check date
542	} else { // end if all numeric inputs
543		return 0;
544	}
545}// end of function
546
547function FormatDateForSQL($DateEntry) {
548
549/* takes a date in a the format specified in $_SESSION['DefaultDateFormat']
550and converts to a yyyy/mm/dd format */
551	$Date_Array = array();
552	$DateEntry = trim($DateEntry);
553
554	if (mb_strpos($DateEntry,'/')) {
555		$Date_Array = explode('/',$DateEntry);
556	} elseif (mb_strpos ($DateEntry,'-')) {
557		$Date_Array = explode('-',$DateEntry);
558	} elseif (mb_strpos ($DateEntry,'.')) {
559		$Date_Array = explode('.',$DateEntry);
560	} elseif (mb_strlen($DateEntry)==6) {
561		$Date_Array[0]= mb_substr($DateEntry,0,2);
562		$Date_Array[1]= mb_substr($DateEntry,2,2);
563		$Date_Array[2]= mb_substr($DateEntry,4,2);
564	} elseif (mb_strlen($DateEntry)==8) {
565		$Date_Array[0]= mb_substr($DateEntry,0,4);
566		$Date_Array[1]= mb_substr($DateEntry,4,2);
567		$Date_Array[2]= mb_substr($DateEntry,6,2);
568	}
569
570	if ($_SESSION['DefaultDateFormat']=='Y/m/d' OR $_SESSION['DefaultDateFormat']=='Y-m-d') {
571		if (mb_strlen($Date_Array[0])==2) {
572			if ((int)$Date_Array[0] <=60) {
573				$Date_Array[0] = '20'.$Date_Array[2];
574			} elseif ((int)$Date_Array[0] >60 AND (int)$Date_Array[2] <100) {
575				$Date_Array[0] = '19'.$Date_Array[2];
576			}
577		}
578		return $Date_Array[0].'-'.$Date_Array[1].'-'.$Date_Array[2];
579
580	} elseif (($_SESSION['DefaultDateFormat']=='d/m/Y')
581				OR $_SESSION['DefaultDateFormat']=='d.m.Y'){
582		if (mb_strlen($Date_Array[2])==2) {
583			if ((int)$Date_Array[2] <=60) {
584				$Date_Array[2] = '20'.$Date_Array[2];
585			} elseif ((int)$Date_Array[2] >60 AND (int)$Date_Array[2] <100) {
586				$Date_Array[2] = '19'. $Date_Array[2];
587			}
588		}
589		/* echo '<BR>The date returned is ' . $Date_Array[2].'/'.$Date_Array[1].'/'.$Date_Array[0]; */
590		return $Date_Array[2].'-'.$Date_Array[1].'-'.$Date_Array[0];
591
592	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y') {
593		if (mb_strlen($Date_Array[2])==2) {
594			if ((int)$Date_Array[2] <=60) {
595				$Date_Array[2] = '20'.$Date_Array[2];
596			} elseif ((int)$Date_Array[2] >60 AND (int)$Date_Array[2] <100) {
597				$Date_Array[2] = '19'.$Date_Array[2];
598			}
599		}
600		return $Date_Array[2].'-'.$Date_Array[0].'-'.$Date_Array[1];
601	}
602
603}// end of function
604
605function FormatDateWithTimeForSQL ($datetime) {
606    //  Split the time off, fix date and add the time to returned value.
607    $dt = explode(' ', $datetime);
608
609    return FormatDateForSQL( $dt[0] ) . ' ' . $dt[1];
610}
611
612function LastDayOfMonth ( $DateEntry) {
613	/*Expects a date in DefaultDateFormat and
614	 * Returns the last day of the month in the entered date
615	 * in the DefaultDateFormat
616	 *
617	 * mktime (0,0,0 month, day, year)
618	 */
619
620	$DateEntry = trim($DateEntry);
621
622	if (mb_strpos($DateEntry,'/')) {
623		$Date_Array = explode('/',$DateEntry);
624	} elseif (mb_strpos ($DateEntry,'-')) {
625		$Date_Array = explode('-',$DateEntry);
626	} elseif (mb_strpos ($DateEntry,'.')) {
627		$Date_Array = explode('.',$DateEntry);
628	} elseif (mb_strlen($DateEntry)==6) {
629		$Date_Array[0]= mb_substr($DateEntry,0,2);
630		$Date_Array[1]= mb_substr($DateEntry,2,2);
631		$Date_Array[2]= mb_substr($DateEntry,4,2);
632	} elseif (mb_strlen($DateEntry)==8) {
633		$Date_Array[0]= mb_substr($DateEntry,0,4);
634		$Date_Array[1]= mb_substr($DateEntry,4,2);
635		$Date_Array[2]= mb_substr($DateEntry,6,2);
636	}
637
638	if ($_SESSION['DefaultDateFormat']=='Y/m/d' OR $_SESSION['DefaultDateFormat']=='Y-m-d') {
639		if (mb_strlen($Date_Array[0])==2) {
640			if ((int)$Date_Array[0] <=60) {
641				$Date_Array[0] = '20'.$Date_Array[2];
642			} elseif ((int)$Date_Array[0] >60 AND (int)$Date_Array[2] <100) {
643				$Date_Array[0] = '19'.$Date_Array[2];
644			}
645		}
646
647		$DateStamp =  mktime(0,0,0, $Date_Array[1]+1, 0, $Date_Array[0]);
648
649	}elseif (($_SESSION['DefaultDateFormat']=='d/m/Y') OR $_SESSION['DefaultDateFormat']=='d.m.Y'){
650		if (mb_strlen($Date_Array[2])==2) {
651			if ((int)$Date_Array[2] <=60) {
652				$Date_Array[2] = '20'.$Date_Array[2];
653			} elseif ((int)$Date_Array[2] >60 AND (int)$Date_Array[2] <100) {
654				$Date_Array[2] = '19'.$Date_Array[2];
655			}
656		}
657		$DateStamp =  mktime(0,0,0, $Date_Array[1]+1, 0, $Date_Array[2]);
658
659
660	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y') {
661		if (mb_strlen($Date_Array[2])==2) {
662			if ((int)$Date_Array[2] <=60) {
663				$Date_Array[2] = '20'.$Date_Array[2];
664			} elseif ((int)$Date_Array[2] >60 AND (int)$Date_Array[2] <100) {
665				$Date_Array[2] = '19'.$Date_Array[2];
666			}
667		}
668		return $Date_Array[2].'-'.$Date_Array[0].'-'.$Date_Array[1];
669		$DateStamp =  mktime(0,0,0, $Date_Array[0]+1, 0, $Date_Array[2]);
670	}
671	return Date($_SESSION['DefaultDateFormat'],$DateStamp);
672}// end of Last Day in the month function
673
674
675
676function Date1GreaterThanDate2 ($Date1, $Date2) {
677
678/* returns true (1) if Date1 is greater than Date2 */
679
680	$Date1 = trim($Date1);
681	$Date2 = trim($Date2);
682
683	/* Get date elements */
684	if ($_SESSION['DefaultDateFormat']=='d.m.Y' )  {
685		list($Day1,$Month1,$Year1) = explode('.', $Date1);
686		list($Day2,$Month2,$Year2) = explode('.', $Date2);
687	} elseif ($_SESSION['DefaultDateFormat'] =='d/m/Y'){
688		list($Day1,$Month1,$Year1) = explode('/', $Date1);
689		list($Day2,$Month2,$Year2) = explode('/', $Date2);
690	} elseif ($_SESSION['DefaultDateFormat'] =='m/d/Y'){
691		list($Month1,$Day1,$Year1) = explode('/', $Date1);
692		list($Month2,$Day2,$Year2) = explode('/', $Date2);
693	} elseif ($_SESSION['DefaultDateFormat'] =='Y/m/d' ){
694		list($Year1,$Month1,$Day1) = explode('/', $Date1);
695		list($Year2,$Month2,$Day2) = explode('/', $Date2);
696	} elseif ($_SESSION['DefaultDateFormat'] =='Y-m-d' ){
697		list($Year1,$Month1,$Day1) = explode('-', $Date1);
698		list($Year2,$Month2,$Day2) = explode('-', $Date2);
699	}
700
701	/*Try to make the year of each date comparable - if one date is specified as just
702	 * 2 characters and the other >2 then then make them both 4 characters long. Assume
703	 *  a date >50 to be 1900's and less than to be 2000's
704	 */
705
706	if (mb_strlen($Year1)>2 AND mb_strlen($Year2)==2){
707		if ($Year2>50) {
708			$Year2=1900+$Year2;
709		} else {
710			$Year2=2000+$Year2;
711		}
712	}
713	if (mb_strlen($Year2)>2 AND mb_strlen($Year1)==2){
714		if ($Year1>50) {
715			$Year1=1900+$Year1;
716		} else {
717			$Year1=2000+$Year1;
718		}
719	}
720
721	/* Compare years */
722	if ($Year1>$Year2){
723		return 1;
724	} elseif ($Year2>$Year1){
725		return 0;
726	}
727
728	/* Compare months. Years are equal*/
729	if ($Month1>$Month2){
730		return 1;
731	} elseif ($Month2>$Month1){
732		return 0;
733	}
734
735	/* Compare days. Years and months are equal */
736	if ($Day1>$Day2){
737		return 1;
738	} elseif ($Day2>$Day1){
739		return 0;
740	}
741	/* The dates are equal, so return false as date 1 is NOT greater than date 2 */
742	return 0;
743}
744
745function CalcDueDate($TranDate, $DayInFollowingMonth, $DaysBeforeDue){
746
747	$TranDate = trim($TranDate);
748
749	if (mb_strpos($TranDate,'/')) {
750		$Date_Array = explode('/',$TranDate);
751	} elseif (mb_strpos ($TranDate,'-')) {
752		$Date_Array = explode('-',$TranDate);
753	} elseif (mb_strpos ($TranDate,'.')) {
754		$Date_Array = explode('.',$TranDate);
755  }
756
757	if (($_SESSION['DefaultDateFormat']=='d/m/Y') OR ($_SESSION['DefaultDateFormat']=='d.m.Y')){
758		if ($DayInFollowingMonth==0){ /*then it must be set up for DaysBeforeDue type */
759
760			$DayDue = $Date_Array[0]+$DaysBeforeDue;
761			$MonthDue = $Date_Array[1];
762			$YearDue = $Date_Array[2];
763
764		} elseif($DayInFollowingMonth>=29) { //take the last day of month
765			if ($DayInFollowingMonth <= 31) {
766				$DayDue = 0;
767			} else {
768				$DayDue = $DayInFollowingMonth-31;
769			}
770			$MonthDue = $Date_Array[1]+2;
771			$YearDue = $Date_Array[2];
772		} else {
773			$DayDue = $DayInFollowingMonth;
774			$MonthDue = $Date_Array[1]+1;
775			$YearDue = $Date_Array[2];
776
777		}
778	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
779		if ($DayInFollowingMonth==0){ /*then it must be set up for DaysBeforeDue type */
780			$DayDue = $Date_Array[1]+$DaysBeforeDue;
781			$MonthDue = $Date_Array[0];
782			$YearDue = $Date_Array[2];
783
784		} elseif($DayInFollowingMonth>=29) { //take the last day of month
785			if ($DayInFollowingMonth <= 31) {
786				$DayDue = 0;
787			} else {
788				$DayDue = $DayInFollowingMonth-31;
789			}
790			$MonthDue = $Date_Array[0]+2;
791			$YearDue = $Date_Array[2];
792		} else {
793			$DayDue = $DayInFollowingMonth;
794			$MonthDue = $Date_Array[0]+1;
795			$YearDue = $Date_Array[2];
796		}
797	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d' OR $_SESSION['DefaultDateFormat']=='Y-m-d'){
798		if ($DayInFollowingMonth==0){ /*then it must be set up for DaysBeforeDue type */
799			$DayDue = $Date_Array[2]+$DaysBeforeDue;
800			$MonthDue = $Date_Array[1];
801			$YearDue = $Date_Array[0];
802
803		} elseif($DayInFollowingMonth>=29) { //take the last day of month
804
805			if ($DayInFollowingMonth <= 31) {
806				$DayDue = 0;
807			} else {
808				$DayDue = $DayInFollowingMonth-31;
809			}
810			$MonthDue = $Date_Array[1]+2;
811			$YearDue = $Date_Array[0];
812		} else {
813			$DayDue = $DayInFollowingMonth;
814			$MonthDue = $Date_Array[1]+1;
815			$YearDue = $Date_Array[0];
816		}
817	}
818	return Date($_SESSION['DefaultDateFormat'], mktime(0,0,0, $MonthDue, $DayDue,$YearDue));
819
820}
821
822function DateAdd ($DateToAddTo,$PeriodString,$NumberPeriods){
823	/*Takes
824	 * DateToAddTo in $_SESSION['DefaultDateFormat'] format
825	 * $PeriodString is one of:
826	 * d - days
827	 * w - weeks
828	 * m - months
829	 * y - years
830	 * $NumberPeriods is an integer positve or negative */
831  $DateToAddTo = trim($DateToAddTo);
832
833	if (mb_strpos($DateToAddTo,'/')) {
834		$Date_Array = explode('/',$DateToAddTo);
835	} elseif (mb_strpos ($DateToAddTo,'-')) {
836		$Date_Array = explode('-',$DateToAddTo);
837	} elseif (mb_strpos ($DateToAddTo,'.')) {
838		$Date_Array = explode('.',$DateToAddTo);
839  }
840
841	if (($_SESSION['DefaultDateFormat']=='d/m/Y') OR ($_SESSION['DefaultDateFormat']=='d.m.Y')){
842
843		switch ($PeriodString) {
844		case 'd': //Days
845			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[0]+$NumberPeriods ,(int)$Date_Array[2]));
846			break;
847		case 'w': //weeks
848			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[0]+($NumberPeriods*7),(int)$Date_Array[2]));
849			break;
850		case 'm': //months
851			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1]+$NumberPeriods,(int)$Date_Array[0],(int)$Date_Array[2]));
852			break;
853		case 'y': //years
854			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[0],(int)$Date_Array[2]+$NumberPeriods));
855			break;
856		default:
857			return 0;
858		}
859	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
860
861		switch ($PeriodString) {
862		case 'd':
863			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[0],(int)$Date_Array[1]+$NumberPeriods,(int)$Date_Array[2]));
864			break;
865		case 'w':
866			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[0],(int)$Date_Array[1]+($NumberPeriods*7),(int)$Date_Array[2]));
867			break;
868		case 'm':
869			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[0]+$NumberPeriods,(int)$Date_Array[1],(int)$Date_Array[2]));
870			break;
871		case 'y':
872			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[0],(int)$Date_Array[1],(int)$Date_Array[2]+$NumberPeriods));
873			break;
874		default:
875			return 0;
876		}
877	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d' OR $_SESSION['DefaultDateFormat']=='Y-m-d'){
878
879		switch ($PeriodString) {
880		case 'd':
881/* Fix up the Y/m/d calculation */
882			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2]+$NumberPeriods,(int)$Date_Array[0]));
883			break;
884		case 'w':
885			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2]+($NumberPeriods*7),(int)$Date_Array[0]));
886			break;
887		case 'm':
888			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1]+$NumberPeriods,(int)$Date_Array[2],(int)$Date_Array[0]));
889			break;
890		case 'y':
891			return Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0]+$NumberPeriods));
892			break;
893		default:
894			return 0;
895		}
896	}
897}
898
899function DateDiff ($Date1, $Date2, $Period) {
900
901/* expects dates in the format specified in $_SESSION['DefaultDateFormat'] - period can be one of 'd','w','y','m'
902months are assumed to be 30 days and years 365.25 days This only works
903provided that both dates are after 1970. Also only works for dates up to the year 2035 ish */
904
905	$Date1 = trim($Date1);
906	$Date2 = trim($Date2);
907
908	if (mb_strpos($Date1,'/')) {
909		$Date1_array = explode('/',$Date1);
910	} elseif (mb_strpos ($Date1,'-')) {
911		$Date1_array = explode('-',$Date1);
912	} elseif (mb_strpos ($Date1,'.')) {
913		$Date1_array = explode('.',$Date1);
914  }
915	if (mb_strpos($Date2,'/')) {
916		$Date2_array = explode('/',$Date2);
917	} elseif (mb_strpos ($Date2,'-')) {
918		$Date2_array = explode('-',$Date2);
919	} elseif (mb_strpos ($Date2,'.')) {
920		$Date2_array = explode('.',$Date2);
921  }
922
923	if (($_SESSION['DefaultDateFormat']=='d/m/Y') or ($_SESSION['DefaultDateFormat']=='d.m.Y')){
924		$Date1_Stamp = mktime(0,0,0, (int)$Date1_array[1],(int)$Date1_array[0],(int)$Date1_array[2]);
925		$Date2_Stamp = mktime(0,0,0, (int)$Date2_array[1],(int)$Date2_array[0],(int)$Date2_array[2]);
926	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
927		$Date1_Stamp = mktime(0,0,0, (int)$Date1_array[0],(int)$Date1_array[1],(int)$Date1_array[2]);
928		$Date2_Stamp = mktime(0,0,0, (int)$Date2_array[0],(int)$Date2_array[1],(int)$Date2_array[2]);
929	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d' OR $_SESSION['DefaultDateFormat']=='Y-m-d'){
930		$Date1_Stamp = mktime(0,0,0, (int)$Date1_array[1],(int)$Date1_array[2],(int)$Date1_array[0]);//Changeorder of entries to match Y/M/D format
931		$Date2_Stamp = mktime(0,0,0, (int)$Date2_array[1],(int)$Date2_array[2],(int)$Date2_array[0]); //Changeorder of entries to match Y/M/D format
932	}
933	$Difference = $Date1_Stamp - $Date2_Stamp;
934
935/* Difference is the number of seconds between each date negative if Date 2 > Date 1 */
936
937	switch ($Period) {
938	case 'd':
939		return (int) ($Difference/(24*60*60));
940		break;
941	case 'w':
942		return (int) ($Difference/(24*60*60*7));
943		break;
944	case 'm':
945		return (int) ($Difference/(24*60*60*30));
946		break;
947	case 's':
948		return $Difference;
949		break;
950	case 'y':
951		return (int) ($Difference/(24*60*60*365.25));
952		break;
953	default:
954		return 0;
955	}
956
957}
958
959
960function CalcEarliestDispatchDate (){
961
962/*There is a system parameter that allows the number of working days to be defined $_SESSION['WorkingDaysWeek'] if this is set to 5 then the assumption is no delivery on Sat and Sun
963 * if the number of working days is 6 then it is assumed that there is no delivery on Sunday
964 * if the number of working days is 7 then there are deliveries every day
965 * */
966
967	$EarliestDispatch = time();
968	/* If the hour is after Dispatch Cut Off Time default dispatch date to tomorrow */
969	$EarliestDispatch = (Date('H')>=$_SESSION['DispatchCutOffTime'])?($EarliestDispatch+24*60*60):$EarliestDispatch;
970
971	if ((Date('w',$EarliestDispatch)==0) AND ($_SESSION['WorkingDaysWeek'] != '7')){
972
973/*if today is a sunday AND the company does NOT work 7 days a week, the dispatch date must be tomorrow (Monday) or after */
974
975		$EarliestDispatch = Mktime(0,0,0,Date('m',$EarliestDispatch),Date('d',$EarliestDispatch)+1,Date('y',$EarliestDispatch));
976
977	} elseif ((Date('w',$EarliestDispatch)==6) AND ($_SESSION['WorkingDaysWeek'] != '6') AND ($_SESSION['WorkingDaysWeek'] != '7')){
978
979/*if today is a saturday AND the company does NOT work at least 6 days a week, the dispatch date must be Monday or after */
980
981		$EarliestDispatch = Mktime(0,0,0,Date('m',$EarliestDispatch),Date('d',$EarliestDispatch)+2,Date('y',$EarliestDispatch));
982
983	}else {
984
985		$EarliestDispatch = Mktime(0,0,0,Date('m'),Date('d'),Date('y'));
986	}
987	return $EarliestDispatch;
988}
989
990function CreatePeriod($PeriodNo, $PeriodEnd) {
991				$GetPrdSQL = "INSERT INTO periods (periodno,
992													lastdate_in_period)
993												VALUES (
994													'" . $PeriodNo . "',
995													'" . Date('Y-m-d', $PeriodEnd) . "')";
996				$ErrMsg = _('An error occurred in adding a new period number');
997				$GetPrdResult = DB_query($GetPrdSQL, $ErrMsg);
998
999}
1000
1001function PeriodExists($TransDate) {
1002
1003	/* Find the date a month on */
1004	$MonthAfterTransDate = Mktime(0,0,0,Date('m',$TransDate)+1,Date('d',$TransDate),Date('Y',$TransDate));
1005
1006	$GetPrdSQL = "SELECT periodno FROM periods WHERE lastdate_in_period < '" . Date('Y/m/d', $MonthAfterTransDate) . "' AND lastdate_in_period >= '" . Date('Y/m/d', $TransDate) . "'";
1007
1008	$ErrMsg = _('An error occurred in retrieving the period number');
1009	$GetPrdResult = DB_query($GetPrdSQL,$ErrMsg);
1010
1011	if (DB_num_rows($GetPrdResult)==0) {
1012		return false;
1013	} else {
1014		return true;
1015	}
1016
1017}
1018
1019function GetPeriod ($TransDate, $UseProhibit=true) {
1020
1021	/* Convert the transaction date into a unix time stamp.*/
1022
1023	if (mb_strpos($TransDate,'/')) {
1024		$Date_Array = explode('/',$TransDate);
1025	} elseif (mb_strpos ($TransDate,'-')) {
1026		$Date_Array = explode('-',$TransDate);
1027	} elseif (mb_strpos ($TransDate,'.')) {
1028		$Date_Array = explode('.',$TransDate);
1029	}
1030
1031	if (($_SESSION['DefaultDateFormat']=='d/m/Y') or ($_SESSION['DefaultDateFormat']=='d.m.Y')){
1032		$TransDate = mktime(0,0,0,$Date_Array[1],$Date_Array[0],$Date_Array[2]);
1033	} elseif ($_SESSION['DefaultDateFormat']=='m/d/Y'){
1034		$TransDate = mktime(0,0,0,$Date_Array[0],$Date_Array[1],$Date_Array[2]);
1035	} elseif ($_SESSION['DefaultDateFormat']=='Y/m/d' OR $_SESSION['DefaultDateFormat']=='Y-m-d'){
1036		$TransDate = mktime(0,0,0,$Date_Array[1],$Date_Array[2],$Date_Array[0]);
1037	}
1038
1039	if (Is_Date(ConvertSQLDate($_SESSION['ProhibitPostingsBefore'])) AND $UseProhibit){ //then the ProhibitPostingsBefore configuration is set
1040		$Date_Array = explode('-', $_SESSION['ProhibitPostingsBefore']); //its in ANSI SQL format
1041		$ProhibitPostingsBefore = mktime(0,0,0,$Date_Array[1],$Date_Array[2],$Date_Array[0]);
1042
1043		/* If transaction date is in a closed period use the month end of that period */
1044		if ($TransDate < $ProhibitPostingsBefore) {
1045			$TransDate = $ProhibitPostingsBefore;
1046		}
1047	}
1048	/* Find the unix timestamp of the last period end date in periods table */
1049	$sql = "SELECT MAX(lastdate_in_period), MAX(periodno) from periods";
1050	$result = DB_query($sql);
1051	$myrow=DB_fetch_row($result);
1052
1053	if (is_null($myrow[0])){ //then no periods are currently defined - so set a couple up starting at 0
1054		$InsertFirstPeriodResult = DB_query("INSERT INTO periods VALUES (0,'" . Date('Y-m-d',mktime(0,0,0,Date('m')+1,0,Date('Y'))) . "')",_('Could not insert first period'));
1055		$InsertFirstPeriodResult = DB_query("INSERT INTO periods VALUES (1,'" . Date('Y-m-d',mktime(0,0,0,Date('m')+2,0,Date('Y'))) . "')",_('Could not insert second period'));
1056		$LastPeriod=1;
1057		$LastPeriodEnd = mktime(0,0,0,Date('m')+2,0,Date('Y'));
1058	} else {
1059		$Date_Array = explode('-', $myrow[0]);
1060		$LastPeriodEnd = mktime(0,0,0,$Date_Array[1]+1,0,(int)$Date_Array[0]);
1061		$LastPeriod = $myrow[1];
1062	}
1063	/* Find the unix timestamp of the first period end date in periods table */
1064	$sql = "SELECT MIN(lastdate_in_period), MIN(periodno) from periods";
1065	$result = DB_query($sql);
1066	$myrow=DB_fetch_row($result);
1067	$Date_Array = explode('-', $myrow[0]);
1068	$FirstPeriodEnd = mktime(0,0,0,$Date_Array[1],0,(int)$Date_Array[0]);
1069	$FirstPeriod = $myrow[1];
1070
1071	/* If the period number doesn't exist */
1072	if (!PeriodExists($TransDate)) {
1073		/* if the transaction is after the last period */
1074
1075		if ($TransDate > $LastPeriodEnd) {
1076
1077			$PeriodEnd = mktime(0,0,0,Date('m', $TransDate)+1, 0, Date('Y', $TransDate));
1078
1079			while ($PeriodEnd >= $LastPeriodEnd) {
1080				if (Date('m', $LastPeriodEnd)<=13) {
1081					$LastPeriodEnd = mktime(0,0,0,Date('m', $LastPeriodEnd)+2, 0, Date('Y', $LastPeriodEnd));
1082				} else {
1083					$LastPeriodEnd = mktime(0,0,0,2, 0, Date('Y', $LastPeriodEnd)+1);
1084				}
1085				$LastPeriod++;
1086				CreatePeriod($LastPeriod, $LastPeriodEnd);
1087			}
1088		} else {
1089		/* The transaction is before the first period */
1090			$PeriodEnd = mktime(0,0,0,Date('m', $TransDate), 0, Date('Y', $TransDate));
1091			$Period = $FirstPeriod - 1;
1092			while ($FirstPeriodEnd > $PeriodEnd) {
1093				CreatePeriod($Period, $FirstPeriodEnd);
1094				$Period--;
1095				if (Date('m', $FirstPeriodEnd)>0) {
1096					$FirstPeriodEnd = mktime(0,0,0,Date('m', $FirstPeriodEnd), 0, Date('Y', $FirstPeriodEnd));
1097				} else {
1098					$FirstPeriodEnd = mktime(0,0,0,13, 0, Date('Y', $FirstPeriodEnd));
1099				}
1100			}
1101		}
1102	} else if (!PeriodExists(mktime(0,0,0,Date('m',$TransDate)+1,Date('d',$TransDate),Date('Y',$TransDate)))) {
1103		/* Make sure the following months period exists */
1104		$sql = "SELECT MAX(lastdate_in_period), MAX(periodno) from periods";
1105		$result = DB_query($sql);
1106		$myrow=DB_fetch_row($result);
1107		$Date_Array = explode('-', $myrow[0]);
1108		$LastPeriodEnd = mktime(0,0,0,$Date_Array[1]+2,0,(int)$Date_Array[0]);
1109		$LastPeriod = $myrow[1];
1110		CreatePeriod($LastPeriod+1, $LastPeriodEnd);
1111	}
1112
1113	/* Now return the period number of the transaction */
1114
1115	$MonthAfterTransDate = Mktime(0,0,0,Date('m',$TransDate)+1,Date('d',$TransDate),Date('Y',$TransDate));
1116	$GetPrdSQL = "SELECT periodno
1117					FROM periods
1118					WHERE lastdate_in_period < '" . Date('Y-m-d', $MonthAfterTransDate) . "'
1119					AND lastdate_in_period >= '" . Date('Y-m-d', $TransDate) . "'";
1120
1121	$ErrMsg = _('An error occurred in retrieving the period number');
1122	$GetPrdResult = DB_query($GetPrdSQL,$ErrMsg);
1123	$myrow = DB_fetch_row($GetPrdResult);
1124
1125	return $myrow[0];
1126}
1127?>
1128