计算一个月的剩余天数

时间:2014-10-03 08:39:03

标签: php

假设我有一个能够选择当月某个日期的用户。如果他愿意,让我们说,选择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; 
}

截至目前,代码非常混乱。我只是没办法绕过这个,这就是我问的原因。

3 个答案:

答案 0 :(得分:9)

您可以使用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

答案 1 :(得分:6)

为什么你这么复杂,使用日期(&#39; t&#39;)来获取你可以做的月份天数,例如:

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

将计算当月的剩余天数。

如果您想从特定日期开始剩余天数

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

答案 2 :(得分:0)

strsplit
相关问题