How can I create a function that can figure out the previous month's last day dates?

Question!

I have a PHP calendar that lists all of the days of the month in a table. Before the first day of the month I have numbers from the prior month and after the last day of the month are the numbers of the days for the upcoming month.

Here's a photo of the Calendar as it currently looks. As you can see the bottom gray numbers are working fine, but the numbers preceding the first day of the month are negative numbers and should instead appear as '29,30'

alt text

The numbers after the last day of the month were simply '32,33,34' for example, so I just created an if statement that checks if the number is greater than the total numbers of days in the current month and if so, then subtract the total numbers of days in the month from '32' for example, which would then make it appear as '1,2,3'.

if ($day > $total_days_of_current_month) {
  echo '<td>' . ($day - $total_days_of_current_month) . ' </td>'; // for example,33-31=2
}

My problem is creating an if statement that somehow knows what the last days of the prior month was. The problem is that some months have 30 days and some have 31 days. Also, the month of February and leap years are a problem. Does anyone know an if statement so i can make it appear as '28,29,30' from the previous month?

By : zeckdude


Answers

Why not just take the timestamp of the first day of the month, and subtract 24 hours from it and then use date('d', $timestamp); ? Also, if you have a timestamp in that month, date('t', $timestamp); will get the number of days in the month for you.

A quick example:

// sets to 3am, first day of month, 3am helps us avoid DST issues with subtracting hours    
$monthStart = mktime(3,0,0,date('n'),1,date('Y')); 
$thisMonth = date('n', $monthStart);

// rewind to the sunday before- date('w') is the "weekday" 0 based    
$date = $monthStart - (date('w',$monthStart) * 60 * 60 * 24);     

while ($date
By : gnarf


Assuming $day is a UNIX timestamp:

// gets you the first of the month
$date = getdate(mktime(0, 0, 0, date('n', $day), 1, date('Y', $day)));

From here, do one of these:

$lastDayOfPrevMonth = strtotime('-1 day', $date[0]);

// or, better, get the first day "in the grid" (assuming week starts on Sunday)
$date = strtotime("-$date[wday] days", $date[0]);

I don't want to harp on it, but have you looked at my previous answer? That's a more robust way to build a calendar instead of all this special case checking.

By : deceze


Take a look at the source of PHP-Calender-Class Project:

http://code.google.com/p/php-calendar-class/

class source: http://code.google.com/p/php-calendar-class/source/browse/trunk/calendar.class.php

It may give you some insight on the problem.

By : bucabay


This video can help you solving your question :)
By: admin