-1

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 
2
  • 2
    you can't fetch a javascript result in php! php is serverside ... so eigher you sent the result of the javascript via ajax or form to your script or use php functions do count the weeks. Commented Jul 13, 2015 at 8:11
  • @donald123 Thank you so much your answer! I will try ajax or find some other way.(php only etc...) Thank you again! Commented Jul 13, 2015 at 8:15

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!I will try Just php way, Because I have to use php and mysql.

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.