Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6

Thread: date()

  1. #1
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    how would one add a month onto a date?

    I would like to display: current month Year, current month-1 Year etc.

    So it should display: Sep 2005, Aug 2005, Jul 2005

    I only need the current month, and the previous 2 months, so it doesnt have to be in a loop....

  2.  

  3. #2
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Try this:

    PHP Code:
    <?php

    $NowDate 
    date("n"mktime());

    $OneMonthAgo $NowDate -1;
    $TwoMonthsAgo $NowDate -2;

    $NowString date("M Y"mktime(0,0,0,$NowDate1));
    $OneMonthAgoString date("M Y"mktime(0,0,0,$OneMonthAgo1));
    $TwoMonthsAgoString date("M Y"mktime(0,0,0,$TwoMonthsAgo1));

    echo 
    "$NowString \n $OneMonthAgoString \n $TwoMonthsAgoString";

    ?>
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  4. #3
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    Thanks ekstreme.

    I sort of got it working. Ive tweaked your code alittle....
    PHP Code:
    //displays Sep 2005
    echo date('M Y');

    //displays Aug 2000
    echo date('M',mktime(0,0,0,date('n'),0,0)); php echo date('Y',mktime(0,0,0,date('n'),0,0));

    //displays Jul 2000
     
    echo date('M',mktime(0,0,0,date('n')-1,0,0)); php echo date('Y',mktime(0,0,0,date('n')-1,0,0)) 
    So Ive got the months displaying correctly, but the years arent working as planned... any suggestions on that side?

    The obvious problem comes in when the current month is Jan 2006, the other dates must display 2005 - so getting the year correct is pretty important...

  5. #4
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    The code I gave you lets the PHPfunctions deal with that. Just use it as I gave it to you

    If you want a test, try this:

    PHP Code:
    <?php
    $NowDate 
    1//set month to Jan

    $OneMonthAgo $NowDate -1;
    $TwoMonthsAgo $NowDate -2;

    $NowString date("M Y"mktime(0,0,0,$NowDate1));
    $OneMonthAgoString date("M Y"mktime(0,0,0,$OneMonthAgo1));
    $TwoMonthsAgoString date("M Y"mktime(0,0,0,$TwoMonthsAgo1));

    echo 
    "$NowString \n $OneMonthAgoString \n $TwoMonthsAgoString";
    ?>
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  6. #5
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    *NOTE TO SELF: next time first test code before editing it*

    Thanks Ekstreme. Its works perfectly. I should've listened the first time round.... :classic:

  7. #6
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801


    Glad it works.
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:53 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com