jribeiro jribeiro - 4 months ago 34
Javascript Question

Send PHP date to javascript date Format

I want to pass my PHP server time to my javascript file.

PHP Code:

date_default_timezone_set('Australia/Perth');
echo date("r");


JS:

$.get('time.php', function(data) {
today = new Date(data);
closing = new Date(data);
});


The php returns Sun, 18 Mar 2012 12:01:23 +0800 which is correct time for Australia/Perth. But this returns an invalid javascript date object.

When I try to convert it to timestamp like:

echo strtotime(date("r"));


I get the javscript date Sun Mar 18 2012 04:03:14 GMT+0000 (WET) (this is the value of today js var)

If I use:

echo gmstrftime('%s');


I get: Sat Mar 17 2012 20:04:30 GMT+0000 (WET).

Can anyone please help me out?

Thanks

Answer

The PHP in nallar's isn't exactly like JavaScript's, this will mimic JavaScript's exactly:

echo date('D M d Y H:i:s O');