-3

I have a general problem however I couldn't do at this time. I work on a project. Our Database is based on Europe/Amsterdam Time. However Client timezones and dates are changing every time. You know what I am sayin' I get the date from database by PHP (CakePHP). And I just want these dates to write "2 minutes ago". We keep the date on database like that "2017-08-04 13:43:38" I can use it for another issues or features. I use timeago.js

My Problem

I am changing any record by interface right now. (2017-08-04 13:43:38) (Istanbul Timezone) The system saves 2017-08-04 12:43:38 (Amsterdam Timezone) as modified date. That's why my javascript always shows that about an hour ago It should be less than minute ago I want to change every single client timezone as Amsterdam timezone.

My Questions:

  1. Where I should do it? (client or server)
  2. I tryna work on clientside. How to equalize timezone shortly by javascript?
Bilal Baraz
  • 135
  • 4
  • 15
  • You need to clarify your question. If you are using the built-in parser on a string like "2017-08-04 13:43:38" it will probably (but not certainly) be treated as local, so represent a different moment in time where host timezone settings vary. Best to keep dates in UTC and parse manually (a library can help). Avoid the built-in parser at all costs, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Aug 05 '17 at 05:38
  • @RobG excuse me. you're right. I just edited my question content. – Bilal Baraz Aug 05 '17 at 07:46

2 Answers2

0

Where I should do it? (client or server)

On the server as it has a host environment whose features and clock accuracy you can be confident of.

I tryna work on clientside. How to equalize timezone shortly by javascript?

If you are going to do it client side, you need to provide a string that will be parsed reliably on the client and that includes the timezone. You can use ISO 8601 format strings and hope that the client parses it correctly, or manually parse the string and make certain (a library can help but isn't mandatory).

Currently Amsterdam is UTC+0200 so you should send a string like "2017-08-04T13:43:38+0200" or "2017-08-04T11:43:38Z" and hope that the client clock is accurate.

If you use a library like moment.js you can get use it to parse the string and get a "friendly" format using fromNow like:

// Using date for Amsterdam
var d = moment("2017-08-05T13:43:38+0200");
console.log(d.format('D MMM YYYY [at] hh:mm A [local]') + ' was ' + d.fromNow() + '.');

// Equivalent UTC
var d = moment("2017-08-05T11:43:38Z");
console.log(d.format('D MMM YYYY [at] hh:mm A [local]') + ' was ' + d.fromNow() + '.');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Be careful with friendly formats, some really dislike them. Seeing "5 days ago" and "last week" when today is Tuesday makes me wonder which was before the other.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

If you want to an accurate time reading you could use an Internet Time Server to guarantee the time is synchronized across your platform with millisecond granularity. some public ones can be found here http://tf.nist.gov/tf-cgi/servers.cgi. I'd use that client side to lookup the time then send it to the server. i believe those servers will serve the time in ISO8601.

kautenja
  • 139
  • 2
  • 6