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>
Related
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
For a realtime countdown i use some php variables and echo them in the js like below:
$expire_year = '2016'; // year
$expire_month = '8'; // month ( 8 = August)
$expire_day = '14'; // day (14th)
$expire_hour = '2'; // hour ( 1-24)
$expire_minutes = '12'; // minutes
i use them in the js like blow:
timestamp = new Date(<?php echo $expire_year; ?>, <?php echo $expire_month - 1; ?>, <?php echo $expire_day; ?>, <?php echo $expire_hour; ?>, <?php echo $expire_minutes; ?>),
This works fine!
I use php strtotime to check if the date really has expired:
$quickpollexpiredate = strtotime("August 14, 2016 2:12"); // set an expiration date
So for the countdown timer in js and for the check in php, i have to use 2 different date "structures".
Is it possible to make a check in php so that i have to use only the 5 variables ($expire_year, $expire_month...) with strtotime() or another function?
So actually what i want to achieve is something like this (ofcourse this is wrong, but i hope you understand what i am trying to achieve)
$quickpollexpiredate = strtotime($expire_year, $expire_month, $expire_day, $expire_hour, $expire_minutes); // set an expiration date
If you have a timestamp in php, you can easily use it to set a Date object in javascript. You just need to consider that a timestamp in javascript is in milliseconds and not seconds like in php:
jsTimestamp = new Date(<?php echo $phpTimestamp; ?> * 1000);
or
jsTimestamp = new Date(<?php echo strtotime($yourDateString); ?> * 1000);
Note that new Date(...) also accepts strings, see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date for more details.
My index.php page includes a config.php file, which returns an array that I have defined some variables in by using "define('var1' , 10)".
I am trying to validate my forms input, but I can't figure out how I can reference var1 from within the JS function. What is the easiest way to do this?
Just echo it to a javascript variable:
<script type="text/javascript">
var var1JS = "<?php echo $var1; ?>";
</script>
Not quite sure I am full understanding without seeing the code but, you could echo the variable from the PHP array in the JS function (as above answer).
Or Echo the entire JS query:
$y = count($PHPdata_array);
echo "function exampleFunction() {";
echo "var ArrayName = [";
for ($i = 1; $i <= $y; $i+=2) {
echo "{" . $PHPdata_array[$i] . "," . $PHPdata_array[$i-1] . "},";
}
echo "];";