请原谅我糟糕的英语。
我想得到'本月有多少周'以及本周的循环次数。 但是我的代码不起作用。(当我在正确的数字中检查php值'$ weekcnt'时,但是没有开始循环。)请帮帮我,告诉我我的问题是什么。
<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
}
?>
结果(谷歌浏览器)
There is 5 Weeks
答案 0 :(得分:1)
你不能这样做...... PHP在服务器端执行,因为只在客户端评估javascript。
你可以单独使用javacript,或者只使用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);