Recording a time with a form

need the right php function parse form values into time values

Page 1 of 1

3 Replies - 736 Views - Last Post: 26 September 2010 - 11:01 AM Rate Topic: -----

#1 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Recording a time with a form

Posted 21 September 2010 - 03:11 AM

Please I could use a little help with recording time from a form and converting it to a php time for storage in a database.



My form has a textfield called starthour, another for startminute and a select list for the meridian(AM/PM). this is my the php code I have so far:

$starthour=mysql_real_escape_string($_REQUEST['starthour']); 
$startminute=mysql_real_escape_string($_REQUEST['startminute']); 
$startmeridian=mysql_real_escape_string($_REQUEST['startmeridian']);
 
$starttime=strtotime($endhour.":".$endminute.":".$endmeridian); 

echo "end time is ".$endtime." start time is ".$starttime;


but it is not working

This post has been edited by nkasei28: 21 September 2010 - 03:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Recording a time with a form

#2 PhunkRabbit  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 406
  • Joined: 23-August 10

Re: Recording a time with a form

Posted 21 September 2010 - 03:17 AM

Use $_POST[''] rather than request, this probally wont solve the error however.

Also, in the echo, you dont need to drop out of the quotes and back in, " allows you to post variables directly where ' does not.
Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Recording a time with a form

Posted 21 September 2010 - 06:10 PM

$starttime=strtotime($endhour.":".$endminute.":".$endmeridian);


Why've you got that second colon in the time string? That'd make it come out to (we'll use the current time as an example) 9:06:PM, which isn't a valid time string, so PHP isn't going to be able to read it into a timestamp. You'll either want to add the seconds, or remove that second colon.

Also, why aren't you validating that the input for your hour, minute, and meridian are correct? Sure, you might write the page to only have the option to pick those specific things, but what's stopping someone else from writing a modified version of the page that submits bad data to the form, thus throwing an error?

Definitely ought to validate the user input there, just to be extra safe.

EDIT: Also you'll note that you're doing strtotime() on variables that aren't initialized at all ($endhour, $endminute, $endmeridian) and you're trying to echo out a variable that isn't initialized, as well ($endtime).

This post has been edited by Valek: 21 September 2010 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 runthis  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Recording a time with a form

Posted 26 September 2010 - 11:01 AM

Hopefully this helps someone
<?
mysql_connect('localhost','name','pass') or die(mysql_error());
mysql_select_db('database');
$time=time();
$q-mysql_query("SELECT time FROM table WHERE id='1'");
$r=mysql_fetch_array($q);
$math=($time - $r['time']);
$start=date('h:i:s a', $time);
$end=date('h:i:s a', $r['time']);
print "
Started - $start <br>
End - $end <br>
$math seconds so far
";
mysql_query("UPDATE table SET time='$time' WHERE id='1'");
?>


not tested

OR

<?
$starthour=mysql_real_escape_string($_GET['starthour']); 
$startminute=mysql_real_escape_string($_GET['startminute']); 
echo "end time is $endtime and start time is $starttime.";
?>


not tested

From what i have been reading i dont think you can include the meridian in strtotime, so that sucks, but you could just add an extra field in your table that says meridian, but im sure im wrong and theres a better way

This post has been edited by runthis: 26 September 2010 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1