genista Posted July 24, 2006 Share Posted July 24, 2006 Hi,I know the following is a bit crude, but I have looked at the manual and am really stuck. I have several join forms, update forms and any other pages where a user can enter a dat. Being in the Uk I want to have the format show dd-mm-yyyy as opposed to mysql's formatting. So what I have done is create a function, called from a functions.php include. The following code gives me an unexpected t variable error@join form:[code=php:0]date($formdate = $date_ofbirth);[/code]functions.php:[code=php:0]function date ($formdate) {$date1 = $formdate$date2 = date("Y-m-d",strtotime($date1)); }[/code]Any ideas on how to do this, I will need several dates to be formatted on each page to add to the confusion!G Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/ Share on other sites More sharing options...
wildteen88 Posted July 24, 2006 Share Posted July 24, 2006 Change[code]date($formdate = $date_ofbirth);[/code]to:[code]date($date_ofbirth);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-62911 Share on other sites More sharing options...
genista Posted July 24, 2006 Author Share Posted July 24, 2006 Ok, I have done that, thanks, the problem is in the function.php file, it is this that is causing the error off the following code:[code=php:0]function date ($formdate) {$date1 = $formdate$date2 = date("Y-m-d",strtotime($date1)); }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-62914 Share on other sites More sharing options...
wildteen88 Posted July 24, 2006 Share Posted July 24, 2006 You are missing a semi-colon (;) at end of this line:[code]$date1 = $formdate[/code]it should be this:[code]$date1 = $formdate;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-62916 Share on other sites More sharing options...
genista Posted July 24, 2006 Author Share Posted July 24, 2006 Ok, my stupid fault. However now I get: "Cannot redeclare date ()" off that code. Would this be off $date1 = $formdate? Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-62921 Share on other sites More sharing options...
wildteen88 Posted July 24, 2006 Share Posted July 24, 2006 No its because your creating a function called data. But there is already a predifined function called date. So change you code to this:[code]function my_date ($formdate) {$date1 = $formdate$date2 = date("Y-m-d",strtotime($date1)); }[/code]The when ever you want to call your data function use my_date Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-62925 Share on other sites More sharing options...
genista Posted July 24, 2006 Author Share Posted July 24, 2006 Ok, the form is loading without error, I will test it and let you know.Thanks again,G Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-62937 Share on other sites More sharing options...
genista Posted July 25, 2006 Author Share Posted July 25, 2006 Ok I have tested it, but I am getting nothing posted to the database, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63301 Share on other sites More sharing options...
designationlocutus Posted July 25, 2006 Share Posted July 25, 2006 You need to return $date2 Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63304 Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 Yeah you need to return $date2 like so:[code]function my_date ($formdate) {$date1 = $formdate$date2 = date("Y-m-d",strtotime($date1)); return $date2;}[/code]Now to use the value returned from your date function you so this:[code]$date = my_date('25-12-2006');[/code]You now use the $date variable within your SQL Query. Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63313 Share on other sites More sharing options...
genista Posted July 25, 2006 Author Share Posted July 25, 2006 Ok, so should I be posting $date rather than $date_ofbirth? By doing the following in my script I still get no post to the database, therefore it makes sense to me that I should $_POST ["$date"] as opposed to date_ofbirth.[code=php:0]function my_date ($formdate) {$date1 = $formdate;$date2 = date("Y-m-d",strtotime($date1)); return $date2;}[/code]join form:[code=php:0]$date = my_date('date_ofbirth');[/code]Thanks g Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63388 Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 Is date_ofbirth the name of a form field? If it is use $_POST['date_ofbirth'] to get the value for that field, like so:[code=php:0]$date = my_date($_POST['date_ofbirth']);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63404 Share on other sites More sharing options...
genista Posted July 25, 2006 Author Share Posted July 25, 2006 Thanks for that, but still not working, this is the entire form as it stands, I can see that:[code=php:0]$date = my_date($_POST['date_ofbirth']);[/code]may conflict with the newuser function as in the code below:[code=php:0]/*Accepting a <form> submission:The $submit variable in the $_POST superglobal array will only be set if the user has clicked on the 'submit'button. This is a reasonable way to check that the user did in fact submit the form.*/if(isset($_POST["submit"])){ // Info has been submitted, check it: // Check login, password and password2 are not empty: # field_validator($field_descr, $field_data, $field_type, $min_length="", $max_length="", $field_required=1) { field_validator("username", $_POST["username"], "alphanumeric", 4, 15); field_validator("password", $_POST["password"], "string", 4, 15); field_validator("confirmation password", $_POST["password2"], "string", 4, 15); // Check that password and password2 match: if(strcmp($_POST["password"], $_POST["password2"])) { // The password and confirmation password didn't match, // Add a message to be displayed to the user: $messages[]="Your passwords did not match"; } /* Checking the login name doesn't already exist in the 'users' table: The idea here is that if an entry already exists in the 'users' db table where 'username' equals $username (ie the login name the user has just provided in the join form), then we return an error message saying that that username is already taken and ask the user to choose another name. */ // build query: line 52 $query="SELECT username FROM users WHERE username='".$_POST["username"]."'"; // Run query: $result=mysql_query($query, $link) or die("MySQL query $query failed. Error if any: ".mysql_error()); // If a row exists with that username, issue an error message: if( ($row=mysql_fetch_array($result)) ){ $messages[]="Username \"".$_POST["username"]."\" already exists. Try another."; }$date = my_date($_POST['date_ofbirth']); /* Creating a new user entry in the users table: If we got here and no error messages were placed in the $messages array above, then the $username that the user provided was valid and we can continue to create an entry in the users table in the mysql db. We also effectively 'login' the user and then forward them to the members.php page using the 'header()' function. */ if(empty($messages)) { // registration ok, get user id and update db with new info: newUser($_POST["username"], $_POST["password"], $_POST["first_name"], $_POST["maiden_name"], $_POST["last_name"], $_POST["address_line1"], $_POST["address_line2"], $_POST["town"], $_POST["county"], $_POST["postcode"], $_POST["daytime_phone"], $_POST["evening_phone"], $_POST["email_address"], $_POST["date_ofbirth"]); // Log the user in: cleanMemberSession($_POST["username"], $_POST["password"]); // and then redirect them to the members page: header("Location: members.php?".session_name()."=".session_id()); /* Note this script stops executing after the header() function above! (the user is forwarded to the members.php page) */ }}/*Below here is HTML interposed with PHP. This HTML is only output ifa. the form hasn't been submittedb. the form was submitted but errors were detected*/?><html><head><title><?php print $title ?></title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><?php doCSS()?></head><body><h1><?php print $title?></h1><?php//Check if $message is set, and output it if it is:if(!empty($messages)){ displayErrors($messages);}?><form action="<?=$_SERVER["PHP_SELF"]?>" method="POST"><table><tr><td>Username:</td><td><input type="text" name="username" value="<?php print isset($_POST["username"]) ? $_POST["username"] : "" ;?>" maxlength="15"></td></tr><tr><td>Password:</td><td><input type="password" name="password" value="" maxlength="15"></td></tr><tr><td>Confirm password:</td><td><input type="password" name="password2" value="" maxlength="15"></td></tr><tr><td>First Name:</td><td><input type="text" name="first_name" value="" maxlength="25"></td></tr><tr><td>Maiden Name:</td><td><input type="text" name="maiden_name" value="" maxlength="25"></td></tr><tr><td>Last Name:</td><td><input type="text" name="last_name" value="" maxlength="25"></td></tr><tr><td>Address Line 1:</td><td><input type="text" name="address_line1" value="" maxlength="25"></td></tr><tr><td>Address Line 2:</td><td><input type="text" name="address_line2" value="" maxlength="25"></td></tr><tr><td>Town:</td><td><input type="text" name="town" value="" maxlength="25"></td></tr>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63411 Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 Is that the full code? As it looks incomplete to me. As your my_date, and a few other functions are nonexistant and half the html is missing. Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63413 Share on other sites More sharing options...
genista Posted July 25, 2006 Author Share Posted July 25, 2006 ok I should explain, the functions are called from functions.php include and I only pasted a bit of the html. Apart from the date formatting everything else is working fine.Thanks,G Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63415 Share on other sites More sharing options...
genista Posted July 26, 2006 Author Share Posted July 26, 2006 I am sure there is a conflict with the newuser function posting the date_ofbirth and the my_date function trying to do the same of the code I have, is this the problem? Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-63877 Share on other sites More sharing options...
genista Posted July 26, 2006 Author Share Posted July 26, 2006 Is the problem above because of the duplications of $_post in [code=php:0] $date = my_date($_POST['date_ofbirth']);[/code]and then in the newuser function:[code=php:0]newUser($_POST["username"], $_POST["password"], $_POST["first_name"], $_POST["maiden_name"], $_POST["last_name"], $_POST["address_line1"], $_POST["address_line2"], $_POST["town"], $_POST["county"], $_POST["postcode"], $_POST["daytime_phone"], $_POST["evening_phone"], $_POST["email_address"], $_POST["date_ofbirth"]);[/code]? Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-64061 Share on other sites More sharing options...
kenrbnsn Posted July 26, 2006 Share Posted July 26, 2006 Your problem is that you store the date from your function in the variable $date, but you're storing the value in $_POST['date_ofbirth'] which is the unchanged data from the form.Try:[code]<?php$date = my_date($_POST['date_ofbirth']);newUser($_POST["username"], $_POST["password"], $_POST["first_name"], $_POST["maiden_name"], $_POST["last_name"], $_POST["address_line1"], $_POST["address_line2"], $_POST["town"], $_POST["county"], $_POST["postcode"], $_POST["daytime_phone"], $_POST["evening_phone"], $_POST["email_address"], $date);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-64065 Share on other sites More sharing options...
genista Posted July 26, 2006 Author Share Posted July 26, 2006 I copied it just as you mentioned above, but insetad of posting it to the date_ofbirth it inserts it into 'details,' details being the previous entry in the newuser part:[code=php:0] newUser($_POST["username"], $_POST["password"], $_POST["first_name"], $_POST["maiden_name"], $_POST["last_name"], $_POST["address_line1"], $_POST["address_line2"], $_POST["town"], $_POST["county"], $_POST["postcode"], $_POST["daytime_phone"], $_POST["evening_phone"], $_POST["email_address"], $_POST["details"], $date); [/code]and on entering a date like so 17-11-1967 it comes out like so:2023-04-03Does this have something to do with strtotime? Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-64121 Share on other sites More sharing options...
genista Posted July 26, 2006 Author Share Posted July 26, 2006 Oh, and if I leave it blank I get "strtotime(): Called with empty time parameter" Quote Link to comment https://forums.phpfreaks.com/topic/15500-date-format-function/#findComment-64127 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.