I am trying to write to a file by using a function with some if statements. The file is created when I add an event to my events calendar. It is 99% functional, but I am having a problem with two of my if statements. The type of file I am writing to is an .ics file that I can import into my Outlook Calendar. I have tested the file and it does work when I write some text to the file. However, I'm writing to the file based on what information was entered into my add event form. Days and months are stored in my calendar without leading zeros. I had to do it this way to get my events calendar to work. However, an .ics file requires leading zeros for dates and months. So, I've created some if statements in my function that add zeros before the day and month if they are less than or equal to 9. Below is my complete function to write to a file. I will explain my issue after the code.
function writeTofile() {
// define variables
$name = mysql_real_escape_string($_POST['name']);
$desc = mysql_real_escape_string($_POST['desc']);
$location = mysql_real_escape_string($_POST['location']);
$file_directory = "../../pages/event_files/"; //the directory you want to store the new file in
$file_name = "$name.ics";//the file's name, stripped of any dangerous tags
$file = $file_directory.$file_name; //this is the entire filename
$fh = fopen($file, 'w') or die("can't open file");
$stringData = "BEGIN:VCALENDAR\n";
fwrite($fh, $stringData);
$stringData = "VERSION:1.0\n";
fwrite($fh, $stringData);
$stringData = "BEGIN:VEVENT\n";
fwrite($fh, $stringData);
if ($_POST['month'] <= 9){
$stringData = "DTSTART:".$_POST['year']."0".$_POST['month']."".$_POST['day']."T".$_POST['time']."\n";
fwrite($fh, $stringData);
}
else if (($_POST['month'] <= 9) && ($_POST['day'] <= 9)){
$stringData = "DTSTART:".$_POST['year']."0".$_POST['month']."0".$_POST['day']."T".$_POST['time']."\n";
fwrite($fh, $stringData);
}
else if ($_POST['day'] <= 9){
$stringData = "DTSTART:".$_POST['year']."".$_POST['month']."0".$_POST['day']."T".$_POST['time']."\n";
fwrite($fh, $stringData);
}
else if ($_POST['day'] >= 10){
$stringData = "DTSTART:".$_POST['year']."".$_POST['month']."".$_POST['day']."T".$_POST['time']."\n";
fwrite($fh, $stringData);
}
else if (($_POST['month'] >= 10) && ($_POST['day'] >= 10)){
$stringData = "DTSTART:".$_POST['year']."".$_POST['month']."".$_POST['day']."T".$_POST['time']."\n";
fwrite($fh, $stringData);
}
if ($_POST['end_month'] <= 9){
$stringData = "DTEND:".$_POST['end_year']."0".$_POST['end_month']."".$_POST['end_day']."T".$_POST['end_time']."\n";
fwrite($fh, $stringData);
}
else if (($_POST['end_month'] <= 9) && ($_POST['end_day'] <= 9)){
$stringData = "DTEND:".$_POST['end_year']."0".$_POST['end_month']."0".$_POST['end_day']."T".$_POST['end_time']."\n";
fwrite($fh, $stringData);
}
else if ($_POST['end_day'] <= 9){
$stringData = "DTEND:".$_POST['end_year']."".$_POST['end_month']."0".$_POST['end_day']."T".$_POST['end_time']."\n";
fwrite($fh, $stringData);
}
else if ($_POST['end_day'] >= 10){
$stringData = "DTEND:".$_POST['end_year']."".$_POST['end_month']."".$_POST['end_day']."T".$_POST['end_time']."\n";
fwrite($fh, $stringData);
}
else if (($_POST['end_month'] >= 10) && ($_POST['end_day'] >= 10)){
$stringData = "DTEND:".$_POST['end_year']."".$_POST['end_month']."".$_POST['end_day']."T".$_POST['end_time']."\n";
fwrite($fh, $stringData);
}
$stringData = "LOCATION;ENCODING=QUOTED-PRINTABLE:$location\n";
fwrite($fh, $stringData);
$stringData = "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:$desc\n";
fwrite($fh, $stringData);
$stringData = "SUMMARY;ENCODING=QUOTED-PRINTABLE:$name\n";
fwrite($fh, $stringData);
$stringData = "PRIORITY:3\n";
fwrite($fh, $stringData);
$stringData = "END:VEVENT\n";
fwrite($fh, $stringData);
$stringData = "END:VCALENDAR\n";
fwrite($fh, $stringData);
fclose($fh);
}
Everything in that function works except for these two if statements:
else if (($_POST['month'] <= 9) && ($_POST['day'] <= 9)){
$stringData = "DTSTART:".$_POST['year']."0".$_POST['month']."0".$_POST['day']."T".$_POST['time']."\n";
fwrite($fh, $stringData);
}
else if (($_POST['end_month'] <= 9) && ($_POST['end_day'] <= 9)){
$stringData = "DTEND:".$_POST['end_year']."0".$_POST['end_month']."0".$_POST['end_day']."T".$_POST['end_time']."\n";
fwrite($fh, $stringData);
}
If the month and the day are less than 9, it is only putting the zero before the month, and not the day. The rest of my statements work and the date format in my .ics file is correct. Does anyone have any ideas why just these two would not work? I realize this probably isn't the best way to do this, but given the way my events table is setup, it's the only way I can get this to work. I hope I explained that clearly. Any thoughts or input is greatly appreciated.
Thank you,
wikedawsum