Send PHP date to JavaScript date format
Solution 1
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');
Solution 2
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 is here: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Solution 3
$.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";
Solution 4
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);
});
Solution 5
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)
jribeiro
Full Stack Web Developer driven by passion, innovation and creativity! I specialise in creating great web and mobile experiences for companies and creatives
Updated on September 15, 2020Comments
-
jribeiro over 3 years
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?
-
jribeiro about 12 yearsIt 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 about 12 yearsI 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!
-
Machavity almost 8 yearsThis doesn't improve or stand out from any of the other answers
-
Peter Griffin over 6 yearsExactly what I was looking for
-
Kevin Beal about 6 yearsThat's no the format I'm seeing in Javascript. I see this format:
date('D M d Y H:i:s \G\M\TO (T)');
-
RobG almost 5 yearsParsing 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.
-
Peter Mortensen over 4 yearsThe link is broken (with a rather amusing 404 PNG animation): "Not Found. We're sorry, we couldn't find what you were looking for."