943,722 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 12950
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 28th, 2009
0

how to get previous month starting and ending date in php

Expand Post »
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..!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nish123 is offline Offline
83 posts
since Apr 2009
Apr 28th, 2009
0

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

i am giving an idea

PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  1. $lastmonth=$current_month-1;

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

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
Apr 29th, 2009
0

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

THnxx brother...!!! but wht to do when current month is 1..?? :O
.
.
.
then previous month become 0..
.
.
.
Anyways thanx for reply..!! tht really helps..!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nish123 is offline Offline
83 posts
since Apr 2009
Apr 29th, 2009
0

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

what to confused..

you can write..
PHP Syntax (Toggle Plain Text)
  1.  
  2. if($current_month==1)
  3. {
  4. $lastmonth=12;
  5. }
  6. else
  7. {
  8. $lastmonth=$current_month-1;
  9. }
Last edited by Shanti C; Apr 29th, 2009 at 1:40 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Apr 29th, 2009
0

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

or one more simple solution to get last month

$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
Apr 29th, 2009
0

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

or one more simple solution to get last month

PHP Syntax (Toggle Plain Text)
  1. $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
Apr 30th, 2009
0

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

what to confused..

you can write..
PHP Syntax (Toggle Plain Text)
  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..!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nish123 is offline Offline
83 posts
since Apr 2009
May 8th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by BzzBee ...
i am giving an idea

PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  1. $lastmonth=$current_month-1;

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

PHP Syntax (Toggle Plain Text)
  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...!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nish123 is offline Offline
83 posts
since Apr 2009
May 8th, 2009
0

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

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.
PHP Syntax (Toggle Plain Text)
  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 C; May 8th, 2009 at 6:43 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 12th, 2010
0
Re: how to get previous month starting and ending date in php
i want to know ending date ,day,month and year accordance with the starting date.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
minnujp is offline Offline
1 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how to add items into cart without payment gateway
Next Thread in PHP Forum Timeline: Where did you learn <?php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC