tags:

views:

183

answers:

3

Hi,

How can i find first day of the next month and remain days to this day from present ?

Thank you

A: 

You can use the php date method to find the current month and date, and then you would need to have a short list to find how many days in that month and subtract (leap year would require extra work).

Elliot
omg, isnt there any short way to do this ? :)
Ahmet vardar
You did nothing with this post other than explain how difficult it is to accomplish the goal. Next time, be more helpful or don't post at all.
mattbasta
+5  A: 

Get the current month and year:

$curMonth = date('n');
$curYear  = date('Y');

Create a timestamp for 00:00 on the first day of next month:

if ($curMonth == 12)
    $firstDayNextMonth = mktime(0, 0, 0, 0, 0, $curYear+1);
else
    $firstDayNextMonth = mktime(0, 0, 0, $curMonth+1, 1);

The number of days til that date is the number of seconds between now and then divided by (24 * 60 * 60).

$daysTilNextMonth = ($firstDayNextMonth - mktime()) / (24 * 3600);

Edit: There you go, tweaked to take account of December. This method is leap-year safe.

Benji XVI
i got 29.895185185185, when i try this
Ahmet vardar
Have edited; try now. Also, you may want to round down to the nearest integer with `floor()`.
Benji XVI
thats amazing, thank you so much sir :
Ahmet vardar
You're very welcome!
Benji XVI
Put the script up at http://ben.am/temp/daysleft.php as a motivator. Quite cool to see the month ticking away!
Benji XVI
+1  A: 

You can get the first of the next month with this:

$now = getdate();
$nextmonth = ($now['mon'] + 1) % 13 + 1;
$year = $now['year'];
if($nextmonth == 1)
    $year++;
$thefirst = gmmktime(0, 0, 0, $nextmonth, $year);

With this example, $thefirst will be the UNIX timestamp for the first of the next month. Use date to format it to your liking.

This will give you the remaining days in the month:

$now = getdate();
$months = array(
    31,
    28 + ($now['year'] % 4 == 0 ? 1 : 0), // Support for leap years!
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31
);
$days = $months[$now['mon'] - 1];
$daysleft = $days - $now['mday'];

The number of days left will be stored in $daysleft.

Hope this helps!

mattbasta
hey thank you !
Ahmet vardar