ramiwahdan Posted March 19, 2020 Share Posted March 19, 2020 I am trying to get the full date from form field using the date function and post method. $fromdate = $_POST[‘sdate’]; $todate = $_POST[‘edate’]; $fromday = date(‘F j, Y, g:i a’,’$fromdate’); $today1 = date(‘F j, Y, g:i a’,’$todate’); When trying to print i get the right format but i get extra information not sure from where? I am getting the date from the form fields of type date. Code Output: March 19, 2020, 4:16 pm,$31202019pm31Asia/Dubai why i am getting the second part! Quote Link to comment https://forums.phpfreaks.com/topic/310325-format-date/ Share on other sites More sharing options...
gw1500se Posted March 19, 2020 Share Posted March 19, 2020 I think if you echo $todate you will get your answer. Quote Link to comment https://forums.phpfreaks.com/topic/310325-format-date/#findComment-1575608 Share on other sites More sharing options...
Phi11W Posted March 19, 2020 Share Posted March 19, 2020 "Danger, Will Robinson!" Your suffering from [Evil] Type Coercion here. $_POST variables are all Strings. Feeding them into the date() function forces PHP to parse and convert the given String value into a Date value, which can have some very confusing consequences. More importantly, though, your quoting is messing things up. PHP doesn't understand the "smart"/sloping quotes that seem to have their way into your Post, so I'm assuming you're not really using those. 🙂 Double-quoted strings have variables inside them expanded. Single-quoted string do not. $x = '10' ; if ( '10' === "$x" ) => true because $x is expanded into the value '10' if ( '10' === '$x' ) => false because '10' != '$x' So, your first call to the date() function really is trying to make a date out of the String value '$fromdate'. Lose the quotes to pass the value of the $fromdate variable. You should never trust User input, so you should be explicitly parsing the POST'ed String values to make sure they represent sensible Date values, and then pass the resulting Date values into the date() function. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/310325-format-date/#findComment-1575609 Share on other sites More sharing options...
ramiwahdan Posted March 19, 2020 Author Share Posted March 19, 2020 10 minutes ago, gw1500se said: I think if you echo $todate you will get your answer. 6 minutes ago, Phi11W said: "Danger, Will Robinson!" Your suffering from [Evil] Type Coercion here. $_POST variables are all Strings. Feeding them into the date() function forces PHP to parse and convert the given String value into a Date value, which can have some very confusing consequences. More importantly, though, your quoting is messing things up. PHP doesn't understand the "smart"/sloping quotes that seem to have their way into your Post, so I'm assuming you're not really using those. 🙂 Double-quoted strings have variables inside them expanded. Single-quoted string do not. $x = '10' ; if ( '10' === "$x" ) => true because $x is expanded into the value '10' if ( '10' === '$x' ) => false because '10' != '$x' So, your first call to the date() function really is trying to make a date out of the String value '$fromdate'. Lose the quotes to pass the value of the $fromdate variable. You should never trust User input, so you should be explicitly parsing the POST'ed String values to make sure they represent sensible Date values, and then pass the resulting Date values into the date() function. Regards, Phill W. thanks for the help, here is what i tried: $fromdate = $_POST['sdate']; $todaten = $_POST['edate']; $fromdate2 = date("d-m-Y",$fromdate); $todate2 = date("d-m-Y",$todate); now getting new error: Notice : A non well formed numeric value encountered in C:\xampp\htdocs\AttendanceSystem\login\reportsbydateforall.php on line 107 please advise? Since POST is getting a string how to convert to date? Quote Link to comment https://forums.phpfreaks.com/topic/310325-format-date/#findComment-1575611 Share on other sites More sharing options...
ginerjm Posted March 19, 2020 Share Posted March 19, 2020 (edited) Assuming that line 107 is where you try and format your input field, what does that input field actually contain? I do believe that is the problem. You can improve this code by trying to convert POST value to a true date type value before trying to format it. RTFM under "strtotime". Here's something to play with: $input1 = '12/01/20'; if ($dt1 = strtotime($input1)) { $result1 = date('Y/m/d',$dt1); echo "Result 1 is: $input1 becomes $result1<br><br>"; } else echo "Invalid date value 1: $input1<br>"; // A slightly valid input $input2 = 'x01/03/20'; if ($dt2 = strtotime($input2)) { $result2 = date('Y/m/d',$dt2); echo "Result 2 is: $input2 becomes $result2<br><br>"; } else echo "Invalid date value 2: $input2<br>"; // A bad input $input3 = '99abc'; if ($dt3 = strtotime($input3)) { $result3 = date('Y/m/d',$dt3); echo "Result 3 is: $input3 becomes $result3<br><br>"; } else echo "Invalid date value 3: $input3<br>"; exit(); Edited March 19, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310325-format-date/#findComment-1575614 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.