2

Is there any way I can return a date using PHP that is the same format that Date.UTC(y,m,d) returns?

Here is an example of the format I need:

1274745600000 (Apr 25, 2010)

3 Answers 3

6

PHP:

$date = '13-09-2010 00:00:00';
date_default_timezone_set('UTC');
echo  (strtotime($date) * 1000) - (strtotime('02-01-1970 00:00:00') * 1000);
//1286928000000

Javascript:

Date.UTC(2010, 9, 13);
//1286928000000
Sign up to request clarification or add additional context in comments.

1 Comment

is there any idea how to get it froom Carbon package?
2

[EDITed]:

The way it returns OUTPUT is the milliseconds from January 1, 1970 to July 8, 2005, according to universal time: which you can get by mktime and appending three 0 like this:

echo mktime(0, 0, 0, 9, 15, 2010).'000';

This will display:

1284508800000


And you can use date function to get in JS UTC INPUT format:

echo date("Y,n,j");

This will display:

2010,9,15


4 Comments

The UTC() method returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time.
I'm trying to return a date in JS UTC format directly from PHP so I don't have to convert it using JS. Is this possible?
Yes, using mktime (the first method), it is possible. It returns the data in UTC format. Try it.
@shamittormar mktime() returns SECONDS. Date.UTC() returns milliseconds in a slightly different format.
0

Inside your function use

date_default_timezone_set('UTC');

This set the default timezone to use. Available since PHP 5.1.And you can simply echo date by date() function.

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.