2014-10-03 175 views
2

因此,假设我有一个用户可以选择一个月中的某个日期。如果他愿意,比方说,选择2014年10月16日,我想将该月的剩余日期显示为日历。计算一个月中的剩余天数

<?php 
error_reporting(0); 

$data = $_POST['input']; 
$days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'); 

$m = date('m'); $y = date('y'); 
$d = cal_days_in_month(CAL_GREGORIAN,$m,$y); 

for ($i=1;$i<;$i++){ 
    echo $i + 1; 
} 

截至目前,代码相当混乱。我无法绕过这一切,这就是为什么我问。

+0

你想获得的剩余天数? – TBI 2014-10-03 08:41:43

+0

剩余天数=月份中的天数 - 选定日期 – deceze 2014-10-03 08:42:16

+0

不是数字。我只是想列出它们。即16日是星期四。我需要输出星期五(17日),星期六(18日),星期日(19日),星期一(20日)等。 – 2014-10-03 08:43:15

回答

7

您可以使用strtotimedate

对于date格式,你可以使用以下命令:天

't'号码在给定月份(28至31
'j'月的一天,没有前导零(1至31

<?php 

$timestamp = strtotime('2014-10-03'); 

$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp); 

var_dump($daysRemaining); // int(28) 

DEMO


编辑:显然,你要列出剩下的日子在一个月:

<?php 

$timestamp = strtotime('2014-10-03'); 
$yearMonth = date('Y-m-', $timestamp); 

$daysInMonth = (int)date('t', $timestamp); 

for ($i = (int)date('j', $timestamp); $i <= $daysInMonth; $i++) { 
    $dateString = date('l \t\h\e jS \o\f F', strtotime($yearMonth . $i)); 

    var_dump($dateString); 
} 

/* 
    string(25) "Friday the 3rd of October" 
    string(27) "Saturday the 4th of October" 
    string(25) "Sunday the 5th of October" 
    string(25) "Monday the 6th of October" 
    string(26) "Tuesday the 7th of October" 
    string(28) "Wednesday the 8th of October" 
    string(27) "Thursday the 9th of October" 
    string(26) "Friday the 10th of October" 
    string(28) "Saturday the 11th of October" 
    string(26) "Sunday the 12th of October" 
    string(26) "Monday the 13th of October" 
    string(27) "Tuesday the 14th of October" 
    string(29) "Wednesday the 15th of October" 
    string(28) "Thursday the 16th of October" 
    string(26) "Friday the 17th of October" 
    string(28) "Saturday the 18th of October" 
    string(26) "Sunday the 19th of October" 
    string(26) "Monday the 20th of October" 
    string(27) "Tuesday the 21st of October" 
    string(29) "Wednesday the 22nd of October" 
    string(28) "Thursday the 23rd of October" 
    string(26) "Friday the 24th of October" 
    string(28) "Saturday the 25th of October" 
    string(26) "Sunday the 26th of October" 
    string(26) "Monday the 27th of October" 
    string(27) "Tuesday the 28th of October" 
    string(29) "Wednesday the 29th of October" 
    string(28) "Thursday the 30th of October" 
    string(28) "Friday the 31st of October" 
*/ 

DEMO

+2

'$ timestamp'是否有意使用? – zerkms 2014-10-03 08:43:14

+0

@zerkms绝对不是 - 谢谢指出! – h2ooooooo 2014-10-03 08:43:41

+0

但这就是剩下的天数。我如何列出这些日子? – 2014-10-03 08:50:25

6

你为什么复杂这么多的使用日期(“T”),以获得在一个月的天数就可以,例如做:

echo date('t') - date('j'); 

将在本月余下数天。

如果你想获得剩余的天数从特定日期使用

$date = strtotime($_POST['input']); 
echo date('t', $date) - date('j', $date);