How can I pass a datetime/timestamp from PHP to javascript. The following does not appear to work:
startLive = new Date(<?php echo date("U", strtotime($start_date)); ?>);
php', function(data) { today = new Date(data); closing = new Date(data); });
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.
The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.
The gmdate() is an inbuilt function in PHP which is used to format a GMT/UTC date and time and return the formatted date strings. It is similar to the date() function but it returns the time in Greenwich Mean Time (GMT). Syntax: string gmdate ( $format, $timestamp )
Try this:
startLive = new Date(<?php echo strtotime($start_date)*1000; ?>);
Explanation:
PHP's strtotime
function returns a Unix timestamp (seconds since 1-1-1970 at midnight).
Javascript's Date()
function can be instantiated by specifying milliseconds since 1-1-1970 at midnight.
So multiply seconds by 1000 and you get milliseconds, which you can use in Javascript.
I think that very simple and more universal solution would be
var dateTime = <?php echo date('c', strtotime($yourDateTime)) ?>;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With