Jump to content

PHP and MySQL Time Tables


Democreous

Recommended Posts

I'm having some difficulty adding a time into a MySQL Time table.  My code will put the time into the correct format, but I am having difficulty inserting it into MySQL. The basic code is as follows...

 

Place a time into a form

 

<? echo ("Time<br>
<select name=\"hh\">");
  
   $hh=0;
   $mm=0;

while($hh<13) // sets loop to go to 12
   {     
   echo ("<option value=\"$hh\">");
   if($hh<10) echo "0"; // puts a zero in front of single numbers to adhere to mysql time format
   echo ("$hh");
   $hh++;
   }
echo ("</select> :<select name=\"mm\">");
while($mm<60) 
   {     

   echo ("<option value=\"$mm\">");
    if($mm<10) echo "0";  // puts a zero in front of single numbers to adhere to mysql time format
echo ("$mm");
   $mm++;
   }
echo ("</select> <select name=\"ampm\"><option value=\"am\">am<option value=\"pm\">pm");

echo ("</select>");

 

Send form result to MySQL


if ($ampm == pm) // Sets To a 24 hour format
{
$hh = ($hh+12);
}
$time=("$hh:$mm:00"); // Puts it into the MySql time format... or so I think
echo ("$time"); // I test to see if it diplays in the correct mysql format, which it does ex. 02:30:00

$con = mysql_connect($HOST, $USER, $PASS); 
if (!$con) 
{ 
  // Since the entire script depends on connection, die if connection fails 
  die("Error connecting to MySQL database!"); 
} 
mysql_select_db($T, $con); 

$sql = "UPDATE calendar_events SET event_title='$title',event_desc='$description',event_date='$idate',event_time='$time' WHERE event_id='$id'"; 
         $result = mysql_query($sql); 
	 echo "<div class=\"ttl\">Event Updated, <a href=\"calendar.php\">Return to main page</a></div>";

}

 

It wont go into the table and it's starting to piss me off. Any Suggestions would be greatly appreciated.

 

- Brian

Link to comment
https://forums.phpfreaks.com/topic/36797-php-and-mysql-time-tables/
Share on other sites

Brian,

 

Can you post your table structure?

 

I belive you should be using the date() function for this purpose. Also, does your form bring back the current record data (less the time) prior to executing the UPDATE query.

 

// set the default timezone to use. Available since PHP 5.1

date_default_timezone_set('UTC');

 

 

// Prints something like: Monday

echo date("l");

 

// Prints something like: Monday 15th of August 2005 03:12:46 PM

echo date('l dS \of F Y h:i:s A');

 

event_date='$idate',  event_time='$time'

// Prints: July 1, 2000 is on a Saturday

echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

 

/* use the constants in the format parameter */

// prints something like: Mon, 15 Aug 2005 15:12:46 UTC

echo date(DATE_RFC822);

 

// prints something like: 2000-07-01T00:00:00+00:00

echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.