Date field in Firefox and IE

Asked

Viewed 576 times

-1

I have the following code:

$dataAssContrato = new DateTime($_POST['dataContrato']);

This line works perfectly in Chrome, but not in firefox or IE, because when I try to register one each, I get the following warning:

Fatal error: Uncaught Exception 'Exception' with message 'Datetime::__Construct() [datetime. -Construct]: Failed to parse time string (25/09/2014) at position 0 (2): Unexpected'

Exception: Datetime::__Construct() [datetime. -Construct]: Failed to parse time string (25/09/2014) at position 0 (2): Unexpected

  • The field is saved in format d/m/Y?

  • I don’t know, I can’t see. I echo to see the format and another warning appears.

  • But the last time I could see it was in the Y-m-d format.

  • Put the form setting, the date field and the version of IE/Firefox that the problem occurs.

3 answers

4

If the date comes from the form in the format d/m/Y convert it to the bank format that is normally Y-m-d.

$dataAssContrato = DateTime::createFromFormat('d/m/Y', trim($_POST['dataContrato']));
$dataFormatada = $dataAssContrato->format('Y-m-d');
  • I put what you suggested and the following warning appeared: Fatal error: Call to a Member Function format() on a non-object

  • Showed the number 10

  • Still the same mistake?

  • Yes. Fatal error: Call to a Member Function format() on a non-object

1

The solution proposed by the lost seems to me ideal, but if you are having difficulty working the object, you can try a less orthodox solution, as follows:

list($d,$m,$y) = explode('/', $_POST['dataContrato']);
$dataAssContrato = new DateTime("$m/$d/$y");
  • I tried your suggestion Eduardo, look what appeared: Notice: Undefined offset: 2 in Notice: Undefined offset: 1 in Fatal error: Uncaught Exception 'Exception' with message 'Datetime::__Construct() [<a href='datetime. --Construct'>datetime. --Construct</a>]: Failed to parse time string (/2014-10-27/) at position 0 (/): Unexpected Character' in Exception: Datetime::__Construct() [<a href='datetime. --Construct'>datetime. -Construct</a>]: Failed to parse time string (/2014-10-27/) at position 0 (/): Unexpected Character in

  • Insert this line at the beginning of your code, maybe your Timezone is not set: date_default_timezone_set("America/Sao_paulo")

  • The same error warning keeps popping up.

  • It’s a hell this IE!!! Nothing he’s accepting!! DIE IE!! Hehehehehehehe

  • Dude, check your php version if it’s not < 5.3. Try this: $dataAssContract = new Datetime(date("c", mktime(0,0,0,$m,$d,$y)));

  • 1

    I was able to adjust that by changing the type of data in the comic book, I switched from datetime to smalldatetime. Something else, really, can be version problem of my php, I am not able to update it to the latest version, because I am using the program Vertrigo to run in my windows, because wammp I could not install here. Nevertheless, thank you very much for your attention.

Show 1 more comment

1

As @Lost already mentioned, it is easier to use the Datetime method::createFromFormat. I also suggest testing the date to make sure there are no errors, so the code would look like the @lost suggestion:

$dataAssContrato = DateTime::createFromFormat('d/m/Y', trim($_POST['dataContrato']));
if (!($dataAssContrato instanceof DateTime)) {
  die('Data Inválida!!'); /* Ou qualquer tratamento que achar necessário */
}
$dataFormatada = $dataAssContrato->format('Y-m-d');

Note that the browser has nothing to do with the code executed on the server side (php in this case). What is probably causing the problem is the formatting of the date fields. It is very likely that you are using an American format Y-m-d in Chrome/Chromium and a Brazilian format d/m/Y in Firefox and IE.

  • But I can format both for Y-m-d and d/m/Y

  • Okay @Gustavosevero, but show us an excerpt of the HTML code that generates these forms to help you better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.