0

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..!!

10
Contributors
15
Replies
32
Views
7 Years
Discussion Span
Last Post by Webiswide
0

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 ;
0

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

0

what to confused..

you can write..

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

or one more simple solution to get last month

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

0

or one more simple solution to get last month

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

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..!!

0

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...!!

0

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;
}
0

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

-1

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')));
0
$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-'

Edited by diafol: fixed formatting

0
$last_month_start = date("Y-m-1", strtotime("last month"));
$last_month_end = date("Y-m-t", strtotime("last month"));

Edited by diafol: fixed formatting

0
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

Edited by diafol: fixed formatting

0

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');

Edited by diafol

0

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

echo "<br/>";

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

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.