31

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

PHP Code:

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

JavaScript:

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

The PHP code 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 JavaScript 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jribeiro
  • 3,387
  • 8
  • 43
  • 70

9 Answers9

61

The PHP code in Luna's answer with echo date isn't exactly like JavaScript code. This will mimic the JavaScript code exactly:

echo date('D M d Y H:i:s O');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
simontemplar
  • 885
  • 1
  • 10
  • 17
13

You could also just leave the PHP code as it is and parse the date using JavaScript:

var date = new Date(Date.parse(DATE));

Then even things like this would work:

new Date(Date.parse('11 March 2017'));

Which outputs via a console log (GMT+1000 is because I am in Australia):

Sat Mar 11 2017 00:00:00 GMT+1000

More information about Date.parse() is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

woytam
  • 35
  • 6
Rob
  • 6,758
  • 4
  • 46
  • 51
  • Parsing unsupported formats with the built-in parser is not a good idea as parsing is implementation dependent and may well fail in unexpected and difficult to find circumstances. – RobG Jul 30 '19 at 21:50
  • 1
    The link is broken (with a rather amusing 404 PNG animation): *"Not Found. We're sorry, we couldn't find what you were looking for."* – Peter Mortensen Sep 14 '19 at 17:54
10
$.get('time.php', function(data) {
  today = new Date(data);
  closing = new Date(data);
});

What was the purpose of multiplying the string by 1000? That operation doesn't make sense.

This PHP will work for that.

echo date('D, d M y H:i:s')." +0000";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luna
  • 1,447
  • 1
  • 18
  • 32
  • It was there to convert from unix timestamp (because of miliseconds) and I forgot to delete. Anyway that returns Sun Mar 18 2012 04:10:32 GMT+0000 (WET) while PHP returns Sun, 18 Mar 2012 12:10:32 +0800. Thanks – jribeiro Mar 18 '12 at 04:12
  • I need my javascript date object to have be set for Sun, 18 Mar 2012 12:10:32 for all the following calculations to work properly! – jribeiro Mar 18 '12 at 04:18
7
date('D M d Y H:i:s O')

It won't work if your current locale isn't English.

A better alternative is to use:

new Date(<? echo date("Y, n - 1, d, H, i, s") ?>)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 5
    WARNING: PHP returns the months numbered from 1-12, but Javascript uses 0-11. This will always give you a month old timestamp. Additionally, one cannot simply subtract a month from the JS date object, since Javascript will "correct" the date upon construction... if you run this code on Jan 31st, Javascript will be given "Feb 31", which it will then correct to be March 3rd (assuming 28 days in Feb). – Aaron Cicali Oct 16 '15 at 02:15
  • Re *"current locale isn't English"*: Do you mean *"current locale isn't US English"*? – Peter Mortensen Sep 14 '19 at 17:41
  • English is a language, not a locale. – RobG Sep 29 '20 at 23:34
5

Here is an example with the DateTime object:

PHP code (works on PHP 5.3 or later)

$serverDate = new \DateTime('NOW');

// If you want to set a different time zone
// $serverDate = new \DateTime('NOW', new \DateTimeZone('Australia/Perth'));
echo $serverDate->format(DATE_ATOM);

JavaScript code

$.get('time.php', function(data) {
  var serverDate = new Date(data);
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
georgiar
  • 379
  • 3
  • 7
3

A good way is timestamp:

echo $data = time()*1000;
echo '
  <div id="setxDatetime">The current server time is: </div>
  <script type="text/javascript">
    var x = document.getElementById("setxDatetime");
    x.innerHTML = x.innerHTML + new Date(' . $data . ');
  </script>
';

1381324693000

The current server time is: Wed Oct 09 2013 16:18:13 GMT+0300 (GTB Standard Time)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Petre Sosa
  • 2,097
  • 1
  • 23
  • 19
1

There might be better solutions, but this one did the trick for me. The key issue is that JavaScript uses months 0-11, while PHP uses 1-12 as mentioned previously.

function convert_date_php_js($date) {
    $converted_date = date("Y", strtotime($date)) . ', ' .
                      (date("n", strtotime($date))-1) . ', ' .
                      date("j", strtotime($date));
    return $converted_date;
}

$priceDate = "2016-09-14";
$d = convert_date_php_js($priceDate);
// Returns 2016, 8, 14
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chad
  • 643
  • 2
  • 11
  • 22
-1

What worked for me was:

<?php $today = date("Y/m/d H:i:s"); ?>

<script>new Date(Date.parse('<?php echo $today; ?>'))</script>
Loosie94
  • 574
  • 8
  • 17
-4

It is very simple:

new Date("<?= date('Y/m/d H:i:s'); ?>");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131