Jump to content

format date


ramiwahdan

Recommended Posts

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!

Link to comment
Share on other sites

"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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by ginerjm
Link to comment
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.