Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating remaining days in a month

Tags:

php

So suppose I have a user that is capable of choosing a certain date in the month. If he would, let's say, choose the 16th of October 2014, I want to display the remaining days of the month as a calendar.

<?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; 
}

Code is pretty messy as of now. I just have no way to go round this, that is why I asked.

like image 335
Jessie Stalk Avatar asked Oct 03 '14 08:10

Jessie Stalk


3 Answers

You can use strtotime and date.

For the format of date you can use the following:

't' Number of days in the given month (28 through 31)
'j' Day of the month without leading zeros (1 to 31)

<?php

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

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

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

DEMO


Edit: Apparently you want to list the remaining days in the month:

<?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

like image 87
h2ooooooo Avatar answered Oct 16 '22 15:10

h2ooooooo


Why you complicate it so much use date('t') to get number of days in month you can do for example:

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

which will count remaining days in current month.

If you want to get remaining days from particular date use

$date = strtotime($_POST['input']);
echo date('t', $date) - date('j', $date);
like image 29
Robert Avatar answered Oct 16 '22 16:10

Robert


$today = new DateTime();

$lastDayOfThisMonth = new DateTime('last day of this month');

$nbOfDaysRemainingThisMonth =  $lastDayOfThisMonth->diff($today)->format('%a days');

echo "Remaining Days Of This Month = ";
echo $nbOfDaysRemainingThisMonth;
like image 22
Anas Nadeem Avatar answered Oct 16 '22 16:10

Anas Nadeem