## 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'

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

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 ```
``` Related Questions Example/How-To create calendar appointment using Exchange 03 Webdav and PHP? Calculate business days Scheduling & DST PHP Calendar Recurrence Logic (adsbygoogle = window.adsbygoogle || []).push({}); Simple Practical Usable PHP Logic for Recurrence javascript and php on the fly calendar Load multiple event feeds with one GData calendar query Calendar using Javascript/ PHP/ mySQL Calendar, offset overlapping events php/mysql: database of bank holidays and events like christmas, easter etc Custom PHP Event Calendar Help Know of any open source zend framework calendar? Custom Event Calendar Google Calendar PHP - Event URL / ID? (Zend_Gdata) Google Calendar PHP Retrieve events by specified date range generate weekly calendar in mysql/php how can i make any numbers above the month's total days appear as new month day numbers? Convert Java's java.util.calendar to PHP code Getting an OOP PHP calendar app to update two tables at once ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com 'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpsh'},{'server':'p3plcpnl0831'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.```