0

Working with Symfony2.0 and jQuery, I have a web application that in certain moment has to save some date data in database.

It works in Safari, Firefox and Chrome for Mac. And it works in Internet explorer, Firefox for Windows.

The problem comes ONLY working with Chrome for Windows.

And I know where the problem comes from, although I don't know how to solve it.

Easy: I get the Date from javascript:

var my_date = new Date();

In the browsers that it works, my_dates values:

Mon Nov 19 2012 21:47:41 GMT+0100 (CET)

In the browser that it doesn't work (just Chrome for Windows), my_dates values:

Mon Nov 19 2012 21:47:41 GMT+0100 (Hora estándar romance)

Then, in the server side, php says:

DateTime::__construct(): Failed to parse time string(Mon Nov 19 2012 21:47:41 GMT+0100 (Hora estándar romance)) at position 40(e): Double timezone specification.

So, on the one hand, Chrome for windows is generating that (Hora estándar romance). And on the other hand PHP is sort of finding that I am passing two timezone specifications.

Anyone knows how to solve this any on client or in server side? (or both)

ElPiter
  • 4,046
  • 9
  • 51
  • 80
  • use a string operation to strip off `(...)` portion, leaving `GMTxxxx`. – Marc B Nov 19 '12 at 20:54
  • 2
    Don't transfer the stringified Date to the server. Use [`Date.now()`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/now) to get the Unix timestamp. – millimoose Nov 19 '12 at 20:56
  • @millimoose: that depends. What if OP wants to keep the timezone info? – zerkms Nov 19 '12 at 20:58
  • [`strtotime`](http://www.php.net/manual/en/function.strtotime.php) bombs on this version? -- Also related: http://stackoverflow.com/questions/6348431/best-way-to-remove-edt-from-a-date-returned-via-javascript-with-tolocalestring – Brad Christie Nov 19 '12 at 20:58
  • @zerkms Then I'd use some library to format a date predictably, but that's extra complexity you might not need. Or there's [`toISOString()`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString) if your browsers support it / with the polyfill. `Date.toString()` is not specified to have a machine-readable result. – millimoose Nov 19 '12 at 20:59
  • @millimoose: btw, `toISOString()` doesn't return the *timezone* (but timezone offset, which is useless if you want to know a timezone). Indeed about `toString()` though – zerkms Nov 19 '12 at 21:05
  • @zerkms If the name of the timezone is the one datum you absolutely require, then yes, you need to resort to imprecise hacks because Javascript's date handling facilities are fairly rudimentary. It's still a situation I'd prefer to avoid. – millimoose Nov 19 '12 at 21:07
  • @millimoose: "It's still a situation I'd prefer to avoid" --- ?? If you need to know a timezone - there is **no** solution other than to have the timezone's name. So it's not "datum" but the only solution – zerkms Nov 19 '12 at 21:08
  • 1
    @zerkms I should've said "a technique I'd prefer to avoid". Whether by nuking the requirement, or using something like [`jsTimezoneDetect`](http://www.pageloom.com/automatic-timezone-detection-with-javascript) – millimoose Nov 19 '12 at 21:15
  • What if, as I finally did, I delete the (...) timezone information?? What if the user client is in Los Angeles and the server is in New York? Won't I be storing a wrong time to the user's eyes? – ElPiter Nov 19 '12 at 21:25

1 Answers1

1

You probably want to pass not the Date object, but rather something like my_dates.toTimeString() or some other conversion to something more machine-readable (like my_dates.valueOf() perhaps.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • A combination of this answer and the first comment on the question sort of solved it for the momento. But I am not sure if it will work in certain cases: What if the user is in Los Angeles (for example), causing that the client browser sends the date over there and the server is in Madrid (also for example). Will it really work? Won't the server store the date in Los Angeles believing that is the time in Madrid. What would be the effect to the user? In my case, both client and servers are in Spain. But what if not?? – ElPiter Nov 19 '12 at 21:23
  • Well, the `valueOf()`, I believe is a number of seconds (or milliseconds?) since epoch UTC. So you just treat it as UTC timestamp. But Generally you shouldn't be using client-side timestamps for anything serious, because it can be easily faked and the machine timers may drift without evil intention. – Michael Krelin - hacker Nov 19 '12 at 21:25
  • Also, just check out all functions available in the date object to see which one suits your needs. – Michael Krelin - hacker Nov 19 '12 at 21:25
  • And, if you need a timezone transferred, there's `getTimezoneOffset` or something like that. – Michael Krelin - hacker Nov 19 '12 at 21:26
  • You've been really helpful. Thanks a lot – ElPiter Nov 19 '12 at 21:34