1

basically I know more or less how to do it but would like to know if there is any better way?

I have the following variables in PHP

$day; $month; $year;

The ones above have values from exploding a php date string.

Below is PHP plugin function which states the date for countdown.

<script type="text/javascript">
$(function () {
    var austDay = new Date();   
    austDay = new Date(2013, 12, 22);
    $('#defaultCountdown').countdown({until: austDay});
});
</script>

I would like to pass the date day/month/year variables into that function from PHP how can I do it, when I tried to attach to the javavariable and put that variable in place of the date part, it didnt work.

Thanks for all help

1
  • 1
    Do it the same way you'd insert any other PHP variable into the HTML document. Commented Nov 20, 2012 at 15:44

3 Answers 3

1
var day  = <?php echo $day ?>; 
var month = <?php echo $month ?>; 
var year = <?php echo $year ?>;

$(function () {
    var austDay = new Date();   
    austDay = new Date( year, month, day );
    $('#defaultCountdown').countdown({until: austDay});
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, been doing this way shows 0,0,0 when touching the year value inside new Date()
What do you mean when "touching" the year value inside new Date() ?
OK every solution works! Thank you. I have gone through the code one more time and corrected one variable, as it was getting the start date, not end date, therefore it was showing 0,0,0.
1

There's a couple of ways you could skin this.

  • Fetch the values via $.ajax with php returning the values as a jsonified array (echo json_encode($my_values))
  • If the page generating the html is a php page then just new Date();
  • Place the values into hidden form fields anywhere on the page or into data-day, data-month, data-year attributes of a relevant object on the page and fetch the values using jquery
    • day = $('#hiddenfield_day').val(); //put the var day into the day field of new date, etc

Hope this helps.

Comments

0

Change this line:

austDay = new Date(<?= $year ?>, <?= $month ?> , <?= $day ?>);

That said, keep in mind that Javascript's new Date() month param takes a number in the 0 - 11 range.

2 Comments

Thanks, this is exactly how I have been doing it, but it shows 0,0,0 when I touch the "YEAR" value, the month and day can be set with <?php echo ""; ?>
OK every solution works! Thank you. I have gone through the code one more time and corrected one variable, as it was getting the start date, not end date, therefore it was showing 0,0,0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.