2

I'm developing a website with CodeIgniter(PHP 7.1.1), in the client side, i need to send some dates to the server:

var date_str = date.toLocaleDateString();
var time_str = date.toLocaleTimeString().split(':');
time_str = time_str[0] + ':' + time_str[1];
...
data_to_ajax = {
    date: inicio,
    date2: termino
};
...

the server will validate those dates using:

DateTime::createFromFormat('d/m/Y H:i', $date);
DateTime::createFromFormat('d/m/Y H:i', $date2);

It works fine, except when using microsoft Edge, createFromFormat will always return false for the date sent. To debug i called var_dump on the dates sent and what i get is:

string(43) "29/08/2017 21:00"
string(31) "29/08/2017 23:30"

While in chrome and mozilla what i get is:

string(16) "29/08/2017 21:00"
string(16) "29/08/2017 23:30"

Well, debugging the http body data sent, chrome and mozilla will send 63 bytes:

date_loc=29%2F08%2F2017+21%3A00&date_ter=29%2F08%2F2017+23%3A30

while edge send 189(63*3) bytes:

date_loc=%E2%80%8E28%E2%80%8E%2F%E2%80%8E08%E2%80%8E%2F%E2%80%8E2017+%E2%80%8E21%E2%80%8E%3A%E2%80%8E00%E2%80%8E&date_ter=%E2%80%8E28%E2%80%8E%2F%E2%80%8E08%E2%80%8E%2F%E2%80%8E2017+23%3A30

Both seems right, because this: url encoder/decoder can decode them, the problems seems to be with the server, why would php show that two strings with the same number of characters have a different length? it is even more weird that var_dump can display those strings normally, but createDateFromFormat will fail.

Calimero
  • 4,238
  • 1
  • 23
  • 34
Paulo Marcio
  • 263
  • 3
  • 13
  • 1
    The combination `%E2%80%8E` which Edge seems to be appending everywhere is a [Left-to-right mark](https://en.wikipedia.org/wiki/Left-to-right_mark) no idea why edge is adding that there though. It seems fairly meaningless when used to that extent – apokryfos Sep 15 '17 at 10:49
  • Is the default language you're using a Right-to-Left language? – apokryfos Sep 15 '17 at 11:06
  • No, i'm using left-to-right – Paulo Marcio Sep 15 '17 at 11:23
  • i just called: `utf8_decode` and the strings have a `?` where edge have put the `LTR-mark`: `string(25) "?29?/?08?/?2017 ?21?:?00?"` seems like `createDateFromFormat` can't handle unicode. – Paulo Marcio Sep 15 '17 at 11:40
  • Hmm, i just changed: `DateTime::createFromFormat('d/m/Y H:i', $date);` to `DateTime::createFromFormat('*d*/*m*/*Y *H*:*i*', $date);` and it worked, got it from the docs: [link](https://secure.php.net/manual/pt_BR/datetime.createfromformat.php) – Paulo Marcio Sep 15 '17 at 11:46
  • Not sure whose responsibility this is. I will guess it's an Edge bug rather than a Chrome bug (because both browsers should have behaved consistently). At any rate that solution looks good so you should post it as an answer when you are able. It might help other people too. – apokryfos Sep 15 '17 at 11:48
  • The only problem is that i need to place the `*` in the format string exactly where edge place the `LRF-mark` or else it will fail, in this case, edge don't place this marks equivalently in the strings, thats why they have different lengths, i will just decode it and remove '?'. Thank you for helping – Paulo Marcio Sep 15 '17 at 11:55
  • 1
    Removing all control characters might also work. Check out https://stackoverflow.com/a/23066553/487813 – apokryfos Sep 15 '17 at 11:57

1 Answers1

1

Microsoft Edge is placing %E2%80%8E in the data sent, wich is a Left-to-right mark, this is causing problems with the php function: createDateFromFormat, because the string sent by Edge don't fit in the format specified: d/m/Y H:i, what you can do is use the *(doc) character in the format string: *d*/*m*/*Y *H*:*i* or you can just remove all control characters(Remove control characters from php String) using:

$date = preg_replace('/[^\PC\s]/u', '', $date);
Paulo Marcio
  • 263
  • 3
  • 13