0

How to put my php variable inside of startDate:'01-01-1996 ?

I want just like this startDate:'01-01-date' How to do that?

$curYear = date('Y');

<script type = "text/javascript"charset = "utf-8">
    $(function () {
    var date = "<?php echo $curYear; ?>";

    $('.date-pick').datePicker({
        startDate: '01-01-1996'
    });
});
</script>
2
  • 1
    Whoa. Are you generating Javascript with PHP? Commented Feb 3, 2014 at 2:51
  • +1, I guess it's startdate.php Commented Feb 3, 2014 at 3:25

4 Answers 4

1

Try

var date = <?php echo $curYear; ?>;
$('.date-pick').datePicker({startDate:date });


Updated after OP's comment

var date = '01-01-' + <?php echo $curYear; ?>;
$('.date-pick').datePicker({startDate:date });

$date = '01-01-'.date('Y'); //in php
var date = <?php echo $date; ?>;
$('.date-pick').datePicker({startDate:date });
Sign up to request clarification or add additional context in comments.

2 Comments

the problem is it start with this day. I want to start it 01-01-year
Sorry What I mean is not working. The clickable button of my calendar was lost.when I change the var date
0

What about this?:

<script type = "text/javascript"charset = "utf-8">
    $(function () {
    var date = "<?php echo $curYear; ?>";

    $('.date-pick').datePicker({
        startDate: '01-01-<?=date("Y")?>'
    });
});
</script>

Comments

0

It's really a very bad idea to mix jQuery with PHP. Try to find another way.

If you need current year:

$(function () {
    var cur_year = new Date().getFullYear();
    $('.date-pick').datePicker({
        startDate: '01-01-'+cur_year
    });
});

If only php script can do it (e.g. if this value is stored in DB) you can use ajax

$(function () {
    $.get('path_to_script_that_returns_year.php', function(resp) {
        $('.date-pick').datePicker({
            startDate: '01-01-'+resp
        });
    });
});

where php script could be

<?php
// ...
// get your year and send
// ...
echo $var_with_year;

Comments

0

if you want to echo variable in javascript or jquery then you want to simply using one line code

<?php echo $variable_name;?>;

Comments

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.