0

I'm currently working on a website that runs on various devices (Apple, Windows, smartphone, tablet, laptop). From my backend PHP script, datetimevalues in string format are sent to my JavaScript. When I run this code on my laptop (Windows-Chrome) I got a different time than when I ran it on my smartphone (Apple - Safari). On my smartphone it shows an hour too much. How can I fix this to get the same values to process in further code? I would like to keep the PHP as it is if possible.

PHP string value:

2019-01-01 01:00:00

On my webpage I've tested this code,

var phpValue = "<?php echo $tijdCountdownPauze; ?>";
var phpValueSplitted = phpValue.split(" "); 
var phpvalueCombine = phpValueSplitted[0] + "T" + phpValueSplitted[1];
var phpValueCombinedDate = new Date(phpvalueCombine);
document.write(phpValueCombinedDate);
document.write("<br>");

Output of Windows laptop in Chrome:

Tue Jan 01 2019 01:00:00 GMT+0100 (Midden-Europese standaardtijd)

Output of Apple iPhone in Safari:

Tue Jan 01 2019 02:00:00 GMT+0100 (CET)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You should use yyyy/MM/dd format, then they become equal. – demkovych Mar 03 '20 at 13:14
  • thanks for the quick reply! unfortunatly this doens't work. maybe I do something wrong..... `const search = '-'; const replaceWith = '/'; var phpValue = ""; var phpValueReplaced = phpValue.split(search).join(replaceWith); var phpValueSplitted = phpValueReplaced.split(" "); var phpvalueCombine = phpValueSplitted[0] + "T" + phpValueSplitted[1]; alert(phpvalueCombine); var countDownDate = new Date(phpvalueCombine); document.write(phpValueOK); ` –  Mar 03 '20 at 14:17
  • @DevanshBaldwa Thank you for capitalizing: there was just a few more to fix, ah ah. You can see my subsequent edit. – Cœur Mar 03 '20 at 14:29
  • @sebastiaan One browser is reading **01:00:00** as local time (GMT+1 for you), the other is reading it as GMT+0 time and then displaying it as local time (GMT+1 for you). The major issue being that you didn't clearly define what was the original timezone from which to interpret the date. – Cœur Mar 03 '20 at 14:35
  • Possible duplicate: https://stackoverflow.com/questions/47898911/convert-time-hhmmss-into-gmt-time-format-in-angular-2 – Cœur Mar 03 '20 at 14:46
  • Possible duplicate: https://stackoverflow.com/questions/3213222/convert-yyyy-mm-dd-hhmmss-ms-gmt-to-local-time-using-javascript – Cœur Mar 03 '20 at 14:51
  • Does this answer your question? [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Cœur Mar 03 '20 at 15:08

1 Answers1

0

Add Z (or any other timezone) to the time string.

phpValueSplitted[0] + "T" + phpValueSplitted[1] + "Z"
// or
phpValueSplitted[0] + "T" + phpValueSplitted[1] + "+00:00"