Jump to content

Help with a function - to do with dates


murfitUK
 Share

Recommended Posts

I'm building a subscription service for a magazine. It is sent out 4 times a year. Issue 96 went out in March 2007. Issue 97 went out in June 2007. Issue 98 went out in September 2007. Issue 99 will go out in December 2007 etc.The function I am trying (and failing miserably) to build will take the issue number and return the month and year that the magazine went out or will go out.I've tried various mathematical functions like ($issue%4)+1 which returns 1,2,3 or 4 which I can then multiply by 3 to get 3, 6, 9 or 12 (ie months March, June, Sept, Dec), but then I get confused about how to deal with the year. I'm sure there's an easier way. Can anyone help?All I want is to pass the issue number to the function, and for it to return the month and year. Eg - send it the number 105 and it will return "June 2009".Thanks.

Link to comment
Share on other sites

Alter your database structure to include 1 column called timestamp.Run this query on your db(assuming it is MySQL):

ALTER TABLE [table_name] ADD COLUMN issue_timestamp int not null;

When you insert items into to the table, insert the value of time()(which should be when you publish the issue, IE the decembe issue will be updated in december, otherwise use Mktime).

Link to comment
Share on other sites

I was hoping there would be an easier way using just php's date & time functions. The mysql table for issues will have only two columns for the issue number (unique, auto-inc, index) and the date is was actually sent out. I know I can insert a third column with the "Due to be posted" info (eg Dec 2007) but thought it unnecessary if the php could work out the month/year with just the issue number.

Link to comment
Share on other sites

Well, you could write an algorithm to do it:

$issue = 96; //What ever issue$year = 1983 + floor($issue / 4);$month = (($issue % 4) + 1) * 3;}

Link to comment
Share on other sites

Thanks everyone. Got it now. I've created the function below:

  function datedue($issue)   {    if (($issue>0) && ($issue<220))        {        $year = 1983 + floor($issue / 4);        $month = (($issue % 4) + 1) * 3;        $due = $year . "-" . $month . "-01";        return date("F Y", strtotime($due));        }    else return "error";   }

So now I can do something like:print datedue(100); and it will print March 2008.I've limited it to issue 219 because after that we get to March 2038 but the time function stops working in Jan 2038. (Wonder what will happen then - will there be scare stories like we had with the millennium bug? Will I be around to care?)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...