Jump to content

Recommended Posts

I have a calendar that users can add events to, and I had it previously so that the hour had to be entered 1 - 24. I wanted it to be 1 - 12 and users can select AM or PM

The code I've done for PM isn't working. Am I making this too complicated?

Thanks!

 

<html>
<head>
<title>Show/Add Events</title>
</head>
<body>
<h1>Show/Add Events</h1>
<?php
$mysqli = mysqli_connect ("97.74.218.110","csteffen","Summer09","csteffen");

//add new event
if ($_POST) {
$m = $_POST["m"];
$d = $_POST["d"];
$y = $_POST["y"];

if ($_POST["event_time_ampm"] = am)
{
$event_date = $y."-".$m."-".$d." ".$_POST["event_time_hh"].":".$_POST["event_time_mm"].":00";
} 
else if ($_POST["event_time_ampm"] = pm)
{
while ($hour = $_POST["event_time_hh"] + 12)
{
$event_date = $y."-".$m."-".$d." ".$hour.":".$_POST["event_time_mm"].":00";
}}

$insEvent_sql = "INSERT INTO calendar_events (event_title, event_shortdesc, event_start) VALUES ('".$_POST["event_title"]."', '".$_POST["event_shortdesc"]."', '$event_date')";
$insEvent_res = mysqli_query($mysqli, $insEvent_sql) or die(mysqli_error($mysqli));
} else {
$m = $_GET["m"];
$d = $_GET["d"];
$y = $_GET["y"];
}

//show events
$getEvent_sql = "SELECT event_title, event_shortdesc, date_format(event_start, '%I:%i %p') AS fmt_date FROM calendar_events WHERE month(event_start) = '".$m."' AND dayofmonth(event_start) = '".$d."' AND year(event_start) = '".$y."' ORDER BY event_start";
$getEvent_res = mysqli_query($mysqli, $getEvent_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($getEvent_res) > 0) {
$event_txt = "<ul>";
while ($ev = mysqli_fetch_array($getEvent_res)) {
$event_title = stripslashes($ev["event_title"]);
$event_shortdesc = stripslashes($ev["event_shortdesc"]);
$fmt_date = $ev["fmt_date"];
$event_txt .= "<li><strong>".$fmt_date."</strong>:
".$event_title."</br>".$event_shortdesc."</li>";
}
$event_txt .= "</ul>";
mysqli_free_result($getEvent_res);
} else {
$event_txt = "";
}

mysqli_close($mysqli);

if ($event_txt != "") {
echo "<p><strong>Today's Events:</strong></p>
$event_txt
<hr/>";
}

if(isset($_COOKIE['ID_staples_clubs']) && isset($_COOKIE['Key_staples_clubs']))
{
//show form
echo "
<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Would you like to add an event?</strong><br/>
Complete the form below and press the submit button to add an event</p>
<p><strong>Event Title:</strong><br/>
<input type=\"text\" name=\"event_title\" size=\"25\" maxlength=\"25\" />
<p><strong>Event Description:</strong><br/>
<input type=\"text\" name=\"event_shortdesc\" size=\"25\" maxlength=\"255\" />
<p><strong>Event Time (hh:mm):</strong><br/>

<input type=\"text\" name=\"event_time_hh\" size=\"2\" maxlength=\"2\" /> :
<input type=\"text\" name=\"event_time_mm\" size=\"2\" maxlength=\"2\" />
<select name=\"event_time_ampm\">
<option value=\"am\">AM</option>
<option value=\"pm\">PM</option>
</select>
<input type=\"hidden\" name=\"m\" value=\"".$m."\">
<input type=\"hidden\" name=\"d\" value=\"".$d."\">
<input type=\"hidden\" name=\"y\" value=\"".$y."\">
<br/><br/>
<input type=\"submit\" name=\"submit\" value=\"Add Event\">
</form>";
}
?>
</body>
</html>
[code]

Link to comment
https://forums.phpfreaks.com/topic/186416-ampm-calendar-issues/
Share on other sites

I have used this array to insert time:

<?php
$times = array("12AM" => "0:", "1AM" => "1:", "2AM" => "2:", "3AM" => "3:", "4AM" => "4:", "5AM" => "5:", "6AM" => "6:",
"7AM" => "7:", "8AM" => "8:", "9AM" => "9:", "10AM" => "10:", "11AM" => "11:", "12PM" => "12:", "1PM" => "13:",
"2PM" => "14:", "3PM" => "15:", "4PM" => "16:", "5PM" => "17:", "6PM" => "18:", "7PM" => "19:", "8PM" => "20:",
"9PM" => "21:", "10PM" => "22:", "11PM" => "23:");
foreach ($times as $k => $v)
{
echo "<option value=\"" .$v . "\">" . $k . "</option>";
}
?>

Then when I called it on my page I used:

//$military is a variable that I retrieved from mysql 
$time = date("g:i A", strtotime($military));

Of course I also used another array with minutes and seconds on it (00:00),(30:00)

Link to comment
https://forums.phpfreaks.com/topic/186416-ampm-calendar-issues/#findComment-984413
Share on other sites

your not treating the am/pm in the if conditionals as strings use 'am' and 'pm'.

also you have a neverending loop

while ($hour = $_POST["event_time_hh"] + 12)
{
$event_date = $y."-".$m."-".$d." ".$hour.":".$_POST["event_time_mm"].":00";
}

as $hr will never be 0, it will cycle through this loop until php timeout is reached (30 secs is default)

 

you might want to consider using mktime function, to validate the fields. and strftime to place it back into string format.

 

Link to comment
https://forums.phpfreaks.com/topic/186416-ampm-calendar-issues/#findComment-984415
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.