Hey frndz i m new member of community..!!
.
.
i jst want to knw is there anyway to get the previous month starting and ending date and which is the previoes month..!!!!
.
.
.
.
hope for positive reply..!!

Recommended Answers

All 15 Replies

i am giving an idea

$current_month=date('m');
$current_year=date('Y');

apply if condition that if current month is greater than one and less and equal to 12 then

$lastmonth=$current_month-1;

offcource the first date will be 1st and you can take end date as

$firstdate= "01/".$lastmonth."/".$current_year ;

$lastdateofmonth=date('t',$lastmonth);// 	this function will give you the number of days in given month(that will be last date of the month)

$lastdate=lastdateofmonth."/".$lastmonth."/".$current_year ;

THnxx brother...!!! but wht to do when current month is 1..?? :O
.
.
.
then previous month become 0..
.
.
.
Anyways thanx for reply..!! tht really helps..!! :)

what to confused..

you can write..

if($current_month==1)
{
$lastmonth=12;
}
else
{
$lastmonth=$current_month-1;
}

or one more simple solution to get last month

$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));

or one more simple solution to get last month

$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));

what to confused..

you can write..

if($current_month==1)
{
$lastmonth=12;
}
else
{
$lastmonth=$current_month-1;
}

thnx maan..!!! i did the same thng..!!
.
.
Query Sloved..!!
.
.
CHeers..!!

i am giving an idea

$current_month=date('m');
$current_year=date('Y');

apply if condition that if current month is greater than one and less and equal to 12 then

$lastmonth=$current_month-1;

offcource the first date will be 1st and you can take end date as

$firstdate= "01/".$lastmonth."/".$current_year ;

$lastdateofmonth=date('t',$lastmonth);// 	this function will give you the number of days in given month(that will be last date of the month)

$lastdate=lastdateofmonth."/".$lastmonth."/".$current_year ;

$lastmonth = date('t',$lastmonth);

this line still show the current month days not last month..!!

any other solution to get last month number of days...!!

Nop, The above logic will give you the correct output unless no mistake in your code..
And see this is another logic:

As we know the first day of the month and for the last month , here i calculated number of days of last month, then that number is last day of the month.

$last_month=date('m')-1;
$year=date('Y');
function get_last_day($year, $last_month){

    $timestamp = strtotime("$year-$month-01");
    $number_of_days = date('t',$timestamp);
    return $number_of_days;
}

i want to know ending date ,day,month and year accordance with the starting date.

This is the easiest way:

$date = new DateTime();
$date->sub(new DateInterval('P1M'));
$dt = $date->format('Y-m');
$first = $dt . '-01';
$last = $dt . date("-t", (strtotime('last day of last month')));
$date = date("Y-m");

$currMounth = date('Y-m', strtotime($date));
$prevMounth1 = date('Y-m', (strtotime($date) - 2400000));
$prevMounth2 = date('Y-m', (strtotime($date) - (2400000 * 2)));
$prevMounth3 = date('Y-m', (strtotime($date) - (2400000 * 3))); 
    // if you want to keep these values into array
$mounths = array($prevMounth1,$prevMounth2,$prevMounth3);

print_r($mounths);

    //if you want to keep only mounth number, just simply remove the 'Y-'
$last_month_start = date("Y-m-1", strtotime("last month"));
$last_month_end = date("Y-m-t", strtotime("last month"));
if (date("n") > 1) $month = mktime(0,0,0,date("n")-1,1,date("Y"));
else $month = mktime(0,0,0,12,1,date("Y")-1);

This is the most accurate way - no errors

Member Avatar for diafol

I think mktime deals correctly with overflows - no need for the conditional.

A DateTime alternative:

$months_ago = 1;

$date = new DateTime();
$date->sub(new DateInterval('P'.$months_ago.'M'));
echo "START : " . $date->format('Y-m-1') . " FINISH: " .  $date->format('Y-m-t');

echo date('Y-m-d', strtotime('first day of last month'));

echo "<br/>";

echo date('Y-m-d', strtotime('last day of last month'));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.