how to get previous month starting and ending date in php

Reply

Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

how to get previous month starting and ending date in php

 
0
  #1
Apr 28th, 2009
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..!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 254
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: how to get previous month starting and ending date in php

 
0
  #2
Apr 28th, 2009
i am giving an idea

  1. $current_month=date('m');
  2. $current_year=date('Y');

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

  1. $lastmonth=$current_month-1;

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

  1. $firstdate= "01/".$lastmonth."/".$current_year ;
  2.  
  3. $lastdateofmonth=date('t',$lastmonth);// this function will give you the number of days in given month(that will be last date of the month)
  4.  
  5. $lastdate=lastdateofmonth."/".$lastmonth."/".$current_year ;
Last edited by BzzBee; Apr 28th, 2009 at 8:24 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: how to get previous month starting and ending date in php

 
0
  #3
Apr 29th, 2009
THnxx brother...!!! but wht to do when current month is 1..?? :O
.
.
.
then previous month become 0..
.
.
.
Anyways thanx for reply..!! tht really helps..!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: how to get previous month starting and ending date in php

 
0
  #4
Apr 29th, 2009
what to confused..

you can write..
  1.  
  2. if($current_month==1)
  3. {
  4. $lastmonth=12;
  5. }
  6. else
  7. {
  8. $lastmonth=$current_month-1;
  9. }
Last edited by Shanti Chepuru; Apr 29th, 2009 at 1:40 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 254
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: how to get previous month starting and ending date in php

 
0
  #5
Apr 29th, 2009
or one more simple solution to get last month

$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 254
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: how to get previous month starting and ending date in php

 
0
  #6
Apr 29th, 2009
or one more simple solution to get last month

  1. $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: how to get previous month starting and ending date in php

 
0
  #7
Apr 30th, 2009
Originally Posted by Shanti Chepuru View Post
what to confused..

you can write..
  1.  
  2. if($current_month==1)
  3. {
  4. $lastmonth=12;
  5. }
  6. else
  7. {
  8. $lastmonth=$current_month-1;
  9. }

thnx maan..!!! i did the same thng..!!
.
.
Query Sloved..!!
.
.
CHeers..!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: how to get previous month starting and ending date in php

 
0
  #8
May 8th, 2009
Originally Posted by BzzBee View Post
i am giving an idea

  1. $current_month=date('m');
  2. $current_year=date('Y');

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

  1. $lastmonth=$current_month-1;

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

  1. $firstdate= "01/".$lastmonth."/".$current_year ;
  2.  
  3. $lastdateofmonth=date('t',$lastmonth);// this function will give you the number of days in given month(that will be last date of the month)
  4.  
  5. $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...!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: how to get previous month starting and ending date in php

 
0
  #9
May 8th, 2009
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.
  1. $last_month=date('m')-1;
  2. $year=date('Y');
  3. function get_last_day($year, $last_month){
  4.  
  5. $timestamp = strtotime("$year-$month-01");
  6. $number_of_days = date('t',$timestamp);
  7. return $number_of_days;
  8. }
Last edited by Shanti Chepuru; May 8th, 2009 at 6:43 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC