GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jan-2003, 02:40
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

How do I get 'last month' in YYYYmm format?


Updated Information: Function to ADD (to) DATE with PHP.



I always assumed this was something simple UNTIL I attempted to actually write it...

If I get the time now (mktime() for instance), how do I get last month in this format: 200212 (i.e. December 2002)?
Last edited by JdS : 25-Aug-2004 at 01:08. Reason: Inserted link to recent / updated information.
  #2  
Old 29-Jan-2003, 04:18
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
getdate() can take a Unix timestamp as input, and is more accurate IMHO

PHP Code:

$time_array = getdate();

$year = $time_array['year'];
$month = $time_array['mon'];  //I do believe this starts at 0, check

if($month == 0){
    $year--;
    $month = 11;
}
else{
    $month--;
}

$formatted = sprintf("%04d%02d", $year, $month); //please check
print $formatted; 



I've got no way to check this, so please debug carefully. Sorry.

GF
  #3  
Old 29-Jan-2003, 04:54
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
How would this work for Jan 2003?

Even if it worked, it would just probably show Dec 2003... or did I miss something in the code?
  #4  
Old 29-Jan-2003, 05:21
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Thumbs up

YES! It does work...


Thanks Garth, it DOES work... I just missed that one line when I looked at your code earlier. Anyway, I made some small changes to your code and so far so good (with the testing)..

PHP Code:

<?php
function pGetLM_yyyymm()
{
  $ta = getdate();
    
  $year = $ta['year'];
  $month = $ta['mon'];
    
  --$month; // go back 1 month
    
  if( $month === 0 ): // if this month is Jan
    --$year; // go back a year
    $month = 12; // last month is Dec
  endif;
    
  // return in format 'YYYYMM'
  return sprintf( $year.'%02d', $month); 
}

?>

  #5  
Old 29-Jan-2003, 05:27
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Cool

Great, C++ hasn't me completely screwed up then

GF
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
150 mb storage only $3.30 a month! WebServeHosting Web Hosting Advertisements & Offers 0 25-Nov-2003 01:28
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 14:03
500MBs of Space, 20 GBs of Traffic $7.95 a month. No contracts. solokron Web Hosting Advertisements & Offers 0 04-Oct-2003 02:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.