?
Solved

PHP - First and last day of last month.

Posted on 2012-09-02
3
Medium Priority
?
1,739 Views
Last Modified: 2012-09-02
Hi, I need to know the first and the last day of the current month and last month.

I use this:

   function firstOfMonth()
                        {
                            return date("Y-m-d H:i:s", strtotime(date('m') . '/01/' . date('Y') . ' 00:00:00'));
                        }
                        function lastOfMonth()
                        {
                            return date("Y-m-d H:i:s", strtotime('-1 second', strtotime('+1 month', strtotime(date('m') . '/01/' . date('Y') . ' 00:00:00'))));
                        }

to find out the first and the last date of the current month, but how do I find out the first and the last day of the last month?

Thank you.
0
Comment
Question by:VAL1N
3 Comments
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 400 total points
ID: 38358678
The first day of a month should be easy, it is allways "01". So your firstOfMonth() function could have been simplified to something like this:

date('Y-m-01 00:00:00')

The PHP date() function has a special format character for finding the last date of a month:

date('Y-m-t 00:00:00')

The "t" format character will insert the number of days of the month, i.e. 28, 29, 30 or 31, depending on the month.

To check previous month (or any month), just provide a date as the second parameter for the date() function:

date('Y-m-t 00:00:00',strtotime('2012-07-01'))

http://no.php.net/manual/en/function.date.php
0
 
LVL 2

Assisted Solution

by:rajshekher
rajshekher earned 400 total points
ID: 38358818
You can use this by passing the $month and $year for finding the first and last date of month.

$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1200 total points
ID: 38358842
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

See: http://www.laprbass.com/RAY_temp_val1n.php

<?php // RAY_temp_val1n.php
error_reporting(E_ALL);
echo '<pre>';

// FROM THE POST AT EE
// Hi, I need to know the first and the last day of the current month and last month.

// REQUIRED PHP 5.1+
date_default_timezone_set('America/Chicago');

// TWO FUNCTION DEFINITIONS
function first_of_month($date = 'TODAY') { return date('Y-m-01', strtotime($date)); }
function  last_of_month($date = 'TODAY') { return date('Y-m-t',  strtotime($date)); }

// TEST CASES
echo PHP_EOL . first_of_month();
echo PHP_EOL . last_of_month();
echo PHP_EOL . first_of_month('-1 month');
echo PHP_EOL . last_of_month('-1 month');
echo PHP_EOL . first_of_month('+1 month');
echo PHP_EOL . last_of_month('+1 month');

Open in new window

HTH, ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question