2016-11-10 20 views
2

出力は、与えられた月と日の日曜日のカウントになります。日曜日のカウント数月と年

は、これは私のコードです:

$months=$_POST['month']; 
$years=$_POST['year'];          
$monthName = date("F", mktime(0, 0, 0, $months)); 
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) . '<br/>'; 
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>'; 

$num_sundays='';     
for ($i = 0; $i < ((strtotime($todt) - strtotime($fromdt))/86400); $i++) 
{ 
    if(date('l',strtotime($fromdt) + ($i * 86400)) == 'Sunday') 
    { 
      $num_sundays++; 
    } 
} 

私は$ num_sundaysエコー場合、私は任意の出力を取得しておりません。私を助けてください 。私はPHP

+0

'のstrtotime($のトッド)' ' – devpro

+2

はこれを削除し、空であるが返されます。 '
' '両方の行とchkの結果から – devpro

+0

私はあまりにもそれが空だと思います –

答えて

0

に新しいです、月内のすべての日曜日を取得したコードの下に以下を参照してくださいあなたはちょうどこの2行から<br>を削除する必要が

function total_sun($month,$year) 
{ 
    $sundays=0; 
    $total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year); 
    for($i=1;$i<=$total_days;$i++) 
    if(date('N',strtotime($year.'-'.$month.'-'.$i))==7) 
    $sundays++; 
    return $sundays; 
} 
echo total_sun(11,2016); 

http://phpio.net/s/l9f

4

$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) . '<br/>'; 
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>'; 

そうでない場合は、この開始日と終了日の一部になり、strtotime()はfalseを返します。

例:

<?php 
$months = 12; 
$years=2016;          
$monthName = date("F", mktime(0, 0, 0, $months)); 
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) . '<br/>'; 
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>'; 

var_dump(strtotime($todt)); 
var_dump(strtotime($fromdt)); 
?> 

DEMO:これは、両方のためにfalseを返します。

例2:

<?php 
$months = 12; 
$years=2016;          
$monthName = date("F", mktime(0, 0, 0, $months)); 
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) ; 
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")); 

var_dump(strtotime($todt)); 
var_dump(strtotime($fromdt)); 
?> 

DEMO:これは値

完全な例を戻します

<?php 
$months = 12; 
$years=2016;          
$monthName = date("F", mktime(0, 0, 0, $months)); 
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) ; 
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")); 

$num_sundays='';     
for ($i = 0; $i < ((strtotime($todt) - strtotime($fromdt))/86400); $i++) 
{ 
    if(date('l',strtotime($fromdt) + ($i * 86400)) == 'Sunday') 
    { 
      $num_sundays++; 
    }  
} 
echo "Total Count is: ".$num_sundays; 
?> 

DEMO:これは4日曜日

0

実施例dorcode calculation

$startd="31-7-2006 15:30:00"; 
$endd="31-7-2007 15:30:00"; 

$startDate=$startd; 
$endDate=$endd; 
$startDate1 = strtotime($startDate); 
$endDate1 = strtotime($endDate); 
if($startDate1>$endDate1) 
{ 
$startDate1 = strtotime($endDate); 
$endDate1 = strtotime($startDate); 
} else { 
$startDate1 = strtotime($startDate); 
$endDate1 = strtotime($endDate); 
} 
$p=0; 
for($i = strtotime("Sunday", $startDate1); $i <= $endDate1; 
$i =strtotime('+1 week', $i))  
{ 
$p++; 
echo $p.": ".date('F d, Y', $i)."<br>"; 
} 
関連する問題