baycisk
baycisk

Reputation: 151

Convert back the time that was converted from js getTime() in PHP

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?

Upvotes: 2

Views: 137

Answers (2)

Not A Robot
Not A Robot

Reputation: 2694

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

Upvotes: 2

Wajid
Wajid

Reputation: 593

You can use the following code,

date('j/m/d H:i A', '1612843200')

Upvotes: 0

Related Questions