1

I have a time array like

[2023,1,3,17,0,0] 

When I use Date.UTC in javascript, It produces the right result

My code

const utcDate1 = new Date(Date.UTC(2023, 1, 3, 17,0,0));
console.log(utcDate1.toUTCString());

result:

"Fri, 03 Feb 2023 17:00:00 GMT"

How to get the same result in php like javascript? I tried with mktime in php but the return result is not what i expected. It differs by one month.

my php code:

echo date("d-m-Y H:i:s", mktime(17,0,0,1,3,2023));

result by PHP :

03-01-2023 17:00:00
10
  • Do you mean to implement this code in php? Commented Feb 4, 2023 at 6:35
  • Have you actually tried anything or done any research? Commented Feb 4, 2023 at 7:49
  • i tried with mktime in php but the return result is not what i expected it to be different when using date.Utc in java script Commented Feb 4, 2023 at 8:08
  • You need to edit the question and show what you tried, then we can help you fix it, or rule it out as a solution. That shows you made an effort before asking for everyone's time, and also usually makes it faster to get to an answer. See also How to Ask. Thanks Commented Feb 4, 2023 at 8:10
  • You need to show the specific code you tried and show the specific result you got. Otherwise it's pretty much meaningless Commented Feb 4, 2023 at 8:18

1 Answer 1

2

Javascript uses a date format that stores months 0 based: month "0" means January.

PHP does not, php considers January to be month 1.

Thats why in js Date.UTC(2023, 1, 3, 17,0,0) represents February 3rd, but in php mktime(17,0,0,1,3,2023) represents January 3rd.

When using that js-style date input [2023,1,3,17,0,0] in php you'll have to +1 to the month value.


If that part of your system is under your control and you have the possibility to change the format you use to exchange date values between js and php, i'd highly recommend to use a format that both systems understand. E.g. ISO 8601 or unix timestamps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.