user3452170
user3452170

Reputation: 57

javascript + php doesn't worked

Please forgive me my lousy English.

I want to get 'How many week in this month' and That number of loop of the week. But my code doesn't worked.(When i checked php value '$weekcnt' in correct number. but doesn't start loop.) Please help me, and tell me what is my problem.

<script>
    //How many week in this month
    var calcWeekCountInTargetYearAndMonthByDate = function(year, month, day) {
        var d = new Date(year, month, day);
        return Math.floor((d.getDate() - d.getDay() + 12) / 7);
    }
    //Get this month's last day
    var calcThisMonthEndDate =  function(year, month){
        var dt = new Date(year, month, 0).getDate();
        return dt;
    }
</script>  
<?php
    //How many week in this month
    $weekcnt = '<script type="text/javascript">document.write(calcWeekCountInTargetYearAndMonthByDate(2015, 6, calcThisMonthEndDate(2015,6)));</script>';
    echo "There is ".$weekcnt." Weeks <br>";
    //number of loop of the week
    for($tablecnt = 0; $tablecnt < $weekcnt; $tablecnt++)
    {
        echo "NOW = ".$tablecnt;
        //Test print
    }
?>

Result(google chrome)

There is 5 Weeks 

Upvotes: 0

Views: 64

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can't do that... PHP is executed in the server side where as the javascript is evaluated only in the client side.

You can do the same using javacript alone, or just with PHP

var calcWeekCountInTargetYearAndMonthByDate = function (year, month, day) {
    var d = new Date(year, month, day);
    return Math.floor((d.getDate() - d.getDay() + 12) / 7);
}
//Get this month's last day
var calcThisMonthEndDate = function (year, month) {
    var dt = new Date(year, month, 0).getDate();
    return dt;
}
var num = calcWeekCountInTargetYearAndMonthByDate(2015, 6, calcThisMonthEndDate(2015, 6));

var text = 'There is ' + num + ' Weeks <br>';
for (var i = 1; i <= num; i++) {
    text += 'NOW = ' + i + '<br />';
}
document.write(text);

Upvotes: 1

Related Questions