2

I have frontend that accept date from calendar, with this format: 9/02/2021 11:00 AM

it then converted into these format: 2021-02-09 11:00, then it converted by this line of code:

var timeStamp = new Date('2021-02-09 11:00').getTime() / 1000;

I know these produce time in this value: 1612843200

Now in backend, i want to read that time value, and want to convert it back into Y-m-d H:i format, i tried date('Y-m-d H:i', '1612843200') but the result is 2021-02-09 04:00

my purpose is to get the time back to date format, and set timezone to UTC later, but i stumble in the first part before set timezone to UTC

how can i get the date back again?

baycisk
  • 131
  • 2
  • 12
  • What I understood from your question that you need to get that ``11:00 AM`` instead of ``04:00`` you are getting when converting seconds to Y-M-D H:M format? Am I correct? – Not A Bot Feb 03 '21 at 05:24
  • yes, the hour missed 7 hours why is that so? – baycisk Feb 03 '21 at 05:27
  • 1
    See what is happening is that, the browser is converting using a particular timezone, and when you are sending those **seconds** to the backend, you are using a particular timezone, thus PHP converting to that timezone that you have set in your backend. **Difference in-between Browser Timezone and Backend Timezone** – Not A Bot Feb 03 '21 at 05:36
  • if that so, how can we fix it? like if we can set timezone in the js before saving into db ? so we know exactly the timezone and use it – baycisk Feb 03 '21 at 05:56
  • You can use ``Intl.DateTimeFormat().resolvedOptions().timeZone`` to get the timezone of the user and send this timezone to the backend, then set ``date_default_timezone_set() `` with that timezone. – Not A Bot Feb 03 '21 at 05:58
  • Another way you can use is that [See this answer](https://stackoverflow.com/a/1091399/11926970) Where you get from the browser in what timezone the user is, then send that value to your backend, and then you can set the timezone accordingly at the backend and do the conversions accordingly. – Not A Bot Feb 03 '21 at 06:01
  • 1
    the Intl.DateTimeFormat().resolvedOptions().timeZone is way to go! i checked, it return the timezone, can you write your answer using Intl.DateTimeFormat().resolvedOptions().timeZone and then change the timezone to utc? i will accept that as correct answer @NotABot, thanks – baycisk Feb 03 '21 at 06:37

2 Answers2

2

You can use Intl.DateTimeFormat to get the timezone of the user.

See the below code to get user timezone.

let userTimeZone =  Intl.DateTimeFormat().resolvedOptions().timeZone;

console.log(userTimeZone);

Then you can send the user timezone, along with the seconds that you are using to your backend server.

There you can set the date_default_timezone_set using the timezone you are getting from the frontend.

See a list of timezone here.

Based on your need you can change the date-time accordingly at the backend.

In your PHP, you can try something like this

$userTimezone = $_POST["userTimeZone"];
$userSeconds = $_POST["seconds"];

date_default_timezone_set('UTC');
//Set this to get UTC timezone
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
0

You can use the following code,

date('j/m/d H:i A', '1612843200')
Wajid
  • 593
  • 3
  • 11
  • This is not what the [Gabriel](https://stackoverflow.com/users/13132856/gabriel-hanriel) is looking for. The question says the difference between the ``Timezone`` of browser and backend – Not A Bot Feb 03 '21 at 05:45