Hi,
How can i find first day of the next month and remain days to this day from present ?
Thank you
Hi,
How can i find first day of the next month and remain days to this day from present ?
Thank you
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).
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.
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!