Results 1 to 8 of 8

Thread: php date

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default php date

    hey,

    I want to find out the date 3 months ago, but in this specific format..
    Ymm. Y is the year such as 6 for 2006.

    So the date now would be 704

    to get this, i used this code:

    PHP Code:
    $dateyear date(y);
    $dateyear str_replace("0"""$dateyear);
    $datemonth date(m);
    $date = ("$dateyear$datemonth"); 
    Anyone got any ideas on how i could do this with the date 3 months ago?? Ive tried using mktime but unsuccessfully..

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <?php
    $y = substr(date('Y'),3,4);
    $m = substr(date('m'),1,2);
    $date = date($y.($m-3).$m);
    echo $date;
    ?>
    The above expression applies to how many months you want to back-track.
    Hope this helps.
    - Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $ystr = date('y');
    $mstr = date('n') - 3;
    if($mstr < 1) {
      $mstr += 12;
      $ystr -= 1;
    }
    
    $tma = mktime(date('G'), date('i'), date('s'), $mstr, date('j'), $ystr);
    
    $tstr = date('y', $tma)[1] . date('m', $tma);
    mburt, that's not quite how date() works. You need to take into account the changing year, whether it's a leap year, exactly how long three months is... March the 7th minus three months is one day later on a leap year than the equivalent operation at another time.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    thanks twey.. got an error msg though..

    Parse error: syntax error, unexpected '[' in /home/crazy4/public_html/xxxx/date.php on line 11

    PHP Code:
    <?php 
    $ystr 
    date('y');
    $mstr date('n') - 3;
    if(
    $mstr 1) {
      
    $mstr += 12;
      
    $ystr -= 1;
    }

    $tma mktime(date('G'), date('i'), date('s'), $mstrdate('j'), $ystr);

    $tstr date('y'$tma)[1] . date('m'$tma);
    $date = ("$tstr");
    echo (
    "$date");
    ?>

  5. #5
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    i believe the "[1]" serves no purpose, you should take it out.
    Why would date be an array?
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    i believe the "[1]" serves no purpose, you should take it out.
    It's vital for the formatting that the OP wanted... I forgot PHP doesn't like array syntax on anything other than variables, though. substr() can be used instead.
    Why would date be an array?
    It's not, it's a string. Array syntax can be used to access specific characters in strings.
    Code:
    $ystr = date('y');
    $mstr = date('n') - 3;
    if($mstr < 1) {
      $mstr += 12;
      $ystr -= 1;
    }
    
    $tma = mktime(date('G'), date('i'), date('s'), $mstr, date('j'), $ystr);
    
    $tstr = substr(date('y', $tma), -1, 1) . date('m', $tma);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You need to take into account the changing year, whether it's a leap year, exactly how long three months is...
    Hehe... Kind of forgot about that.
    - Mike

  8. #8
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    works great! thanks guys!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •