i am receving start and end dates from a proc in following format
Array
(
[0] => Array
(
[min_created_date] => 2020-10-28 00:00:00.000
[max_created_date] => 2020-11-11 00:00:00.000
)
)
i have stored the values in below variables with the following outputs resepectively :
$start_range =$slider_range[0]['min_created_date'];
$start_range=substr($start_range, 0, strrpos($start_range, ' '));
$end_range= $slider_range[0]['max_created_date'];
$end_range=substr($end_range, 0, strrpos($end_range, ' '));
2020-10-28
2020-11-11
now i need to pass these dates to a jQuery function , i want to store this date to js variable since few js opetaions have to be performed to these dates further , Below is how i am trying to pass the dates
$(function() {
var startrange = <?php echo $start_range;?>;
var endrange = <?php echo $end_range;?>;
console.log(startrange+'startrange');
console.log(endrange+'endrange');
});
I get following in console :
1982startrange
1998endrange
please guide me how can i pass the dates to JS variables instead of <?php echo $date ?>
You have no other way to publish PHP variables in JavaScript without echo.
A cleaner way to do it to assign PHP variables to JavaScript vars inside a <script> tag so that PHP echo are not everywhere in your JS code.
<script type="text/javascript">
var startrange = '<?php echo $start_range; ?>';
var endrange = '<?php echo $end_range; ?>';
</script>
you have a lot of options, but i love unixtimestamp formats more ^^
PHP
$minCreatedDate = time();
$maxCreatedDate = strtotime('2020-11-11 00:00:00.000');
JS echoed in PHP file
var startrange = new Date(<?= $minCreatedDate ?> * 1000);
Date instance contains a lot of funcs. like "getDay", ...
I am attempting the below to pass the value in my php variable to JavaScript, but I never have the alert display on screen. What is the proper way to do such?
PHP
<?php $date = '20170101'; ?>
JavaScript
var datestring = <? php echo $date; ?>;
var year = datestring.substring(0,4);
alert(year);
Fiddle:
https://jsfiddle.net/3ed6opj3/
The desired result that I want to display in the alert is 2017 - the answers below to this point have all been showing <? p
You have to add " on between the php script
var datestring = "<?php echo $date; ?>";
You need to add quotes
<?php echo "\"".$date."\""; ?>;
I am using the Jquery "Final Countdown" Timer (found here: http://hilios.github.io/jQuery.countdown/) but I am modifying it to countdown from a date which is specified from a php var, like so:
PHP:
$gametimestamp = date("Y/m/d H:i:s", strtotime($gametimestamp));
Javascript:
var plustime = new Date(<?php echo $gametimestamp; ?>);
plustime.setSeconds(plustime.getSeconds() + 30);
$("#timerinsert")
.countdown(plustime, function(event) {
$(this).text(
event.strftime('%M:%S')
);
});
If
var plustime = new Date(<?php echo $gametimestamp; ?>);
Outputs like:
var plustime = new Date(2017/03/04 20:19:10);
Then why is the countdown timer not displaying? It DOES work if I just use
date();
You have to add quotes when echoing strings to javascript
var plustime = new Date("<?php echo $gametimestamp; ?>");
And note that new Date accepts a certain format, preferably you'd echo seconds from epoch instead, and add three zeros
$gametimestamp = strtotime($gametimestamp); // Unix timestamp in seconds
and then
var plustime = new Date(<?php echo $gametimestamp; ?> * 1000); // in milliseconds
I'm using the jQuery countdown plugin to have a timer on my website
http://keith-wood.name/countdownRef.html
I am using an API from SportsRadar to list some fixtures that has the dates on
<?php echo date("Y/d/m/ - H:i", strtotime($nextMatch['KickOff'])); ?>
this will output as 23/04/2015 - 20:00
In the Countdown plugin their function is the following
<script>
var matchDay = new Date();
matchDay = new Date(2015, 04-1, 22, 20, 0, 0);
</script>
I'm just looking to know how would I add that PHP echo into that JavaScript function? Is it even possible?
You an use something like:
var matchDay = new Date(<?php $time = strtotime($nextMatch['KickOff']);
echo date("Y", $time) . "," . date("m", $time) . "," .
date("m", $time) . "," . date("H", $time) . "," .
date("i", $time);
?>, 0, 0);
Or you an parse date in JavaScript
PHP function strtotime() gives you the number of seconds since 1970. You can use this information to initialize a javascript Date object. However, javascript expects the number of milliseconds since 1970, thus you ought to multiply the value by 1000:
<script>
var matchDay = new Date(<?php echo strtotime($nextMatch['KickOff'])*1000;?>);
</script>
Now you know when the match will take place (in javascript), and you can use it to initialize a countdown or whatever else you want to do with this information.
PHP is server-side and will run once. JS is much unlike this. If you need the JS to call a PHP script, consider making an AJAX call. Looking at your goals, however, this seems a tad unnecessary. If all you want is to echo data, you can use JS to update HTML elements on the page instead. That I believe would be the simple solution for your requirements.
By doing this it worked fine
<script>
var matchDay = new Date();
matchDay = new Date('<?php echo date("Y", strtotime($nextMatch['KickOff'])); ?>', '<?php echo date("m", strtotime($nextMatch['KickOff'])); ?>'-1, '<?php echo date("d", strtotime($nextMatch['KickOff'])); ?>', '<?php echo date("H", strtotime($nextMatch['KickOff'])); ?>','<?php echo date("i", strtotime($nextMatch['KickOff'])); ?>');
</script>
basically im trying to get equivalent of php time() in javascript
here is the code :
var jstamp = Math.round(new Date().getTime() / 1000) ;
var pstamp = <?php echo time(); ?>;
console.log( 'jstamp is : '+ jstamp);
console.log( 'pstamp is : '+ pstamp);
here is the result :
jstamp is : 13939 45587
pstamp is : 13939 33954
they are way different in the last 5 digits
echo date_default_timezone_get();
echo date('Y-m-d H:i:s');
result :
UTC2014-03-04 11:57:13
server time seems to be wrong , but shouldn't be wrong for both php and js ? meaning that still shouldn't i get the same result in both of them ?
It will probably be the timezone of your server. You can manually set it in PHP: http://php.net/manual/de/function.date-default-timezone-set.php