HTML Tutorial


 /help/HTML Help Forum   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
Every other week date
Post new topic   Reply to topic    HTML Help Forum -> PHP
View previous topic :: View next topic  
Author Message
curtranhome



Joined: 04 Jun 2008
Posts: 77

PostPosted: Tue Nov 17, 2009 9:22 pm     Every other week date Reply with quote

is there any way to display the date 2 weeks in advance? like today is the 17th, can we display the date on the 1st of december? and the the 15th, then the 29th, etc. without having to manually update it?
kanenas



Joined: 14 Dec 2004
Posts: 341

PostPosted: Thu Dec 10, 2009 8:32 pm     Need more info Reply with quote

It depends entirely how you're generating the date. Any scripting language that supports dates should have a way of specifying offsets for dates & times, so you can generate a date a given distance into the future or past.
curtranhome



Joined: 04 Jun 2008
Posts: 77

PostPosted: Thu Dec 10, 2009 9:58 pm     RE: Reply with quote

ok well thanks, but i'm more of a newbie when it comes to server side scripting, in other words, how do i do something like that?
kanenas



Joined: 14 Dec 2004
Posts: 341

PostPosted: Fri Dec 11, 2009 7:22 pm     Need more info Reply with quote

You should always describe the relevant portions of the environment. What scripting languages do you have available (including versions)? For example, in PHP you can generate a timestamp two weeks into the future with either:
Code:
$fortnight = strtotime('+2 weeks');
$fortnight = time() + 60*60*24*7*2;

and print it using:
Code:
strftime('%B %e, %Y', $fortnight);
date('F jS, Y', $fortnight);

Read "Date/Time Functions" in the PHP documentation for details. As of PHP 5.3, you can also use all of the DateTime classes (some are available in earlier versions).
curtranhome



Joined: 04 Jun 2008
Posts: 77

PostPosted: Sat Dec 12, 2009 8:46 pm     RE: Reply with quote

I'm not sure on the complete list of available languages, but, so far, i do know that i can run anything but mySQL. not meaning to be a problem, but is there a way to set a certain date and have the script build off of that, like say i set a date to today (12/12/09) then i insert a code that generates the date 2 weeks from that date, then display, then when that day comes, it will set it as the date to add another 2 weeks. is there a way to do anything like that? sorry if its hard to understand Crying or Very sad

Thanks ahead of time
~curtranhome
kanenas



Joined: 14 Dec 2004
Posts: 341

PostPosted: Mon Dec 14, 2009 7:20 am     Use rounding functions Reply with quote

There are two approaches: store the next date somewhere (preferably a database), updating it when the date arrives, or calculate the difference between the current date and a reference date and use a round-to-nearest, floor, ceiling or truncation. I'll call all of these "rounding functions".

Let's examine the second option. Here's the breakdown.

  • We want our calculation relative to a specific date, so we start by subtracting this base date, and add it back at the end
  • Rounding x to a multiple of n is done by: "round(x / n) * n". Substituting floor, ceiling or truncation for "round" in this formula will perform that appropriate function on x to the nearest n.
  • We want the next fortnight, and timestamps are expressed in seconds, so we're rounding to 60*60*24*14 seconds (the number of seconds in a fortnight).
  • Ceiling (which gives the nearest later fortnight) may seem the natural rounding function to get the next fortnight, but it gives the wrong value when a date is on the fortnight. Instead, find the floor (the nearest earlier fortnight) and add one. You could also calculate the nearest earlier fortnight and add a fortnight. The two are equivalent, due to the distributive property:
    Code:
    floor(x/n + 1) * n = (floor(x/n) + 1) * n = floor(x/n)*n + n

    Use whichever makes more sense to you.


This leads to:
Code:
function fortnightly($now, $base) {
   static $fortnight = 1209600; //=60*60*24*14;
   $offset = (floor(($now - $base) / $fortnight)+1) * $fortnight;
   return $base + $offset;
}

where $now, $base and the return value are timestamps:

Code:
echo strftime('%F', fortnightly(now(), strtotime('2009-12-14 00:00:00')));
curtranhome



Joined: 04 Jun 2008
Posts: 77

PostPosted: Mon Dec 14, 2009 2:41 pm     RE: Reply with quote

Ok thanks, and not to be a pest or anything but how is this supposed to be put together? cause after the function, the echo makes the code useless.

Thanks,
curt
kanenas



Joined: 14 Dec 2004
Posts: 341

PostPosted: Wed Dec 16, 2009 9:46 pm     What do you mean? Reply with quote

I'm not sure I follow. If you don't want to echo the date, don't echo it. Even the function fortnightly is merely an example. Adapt it as necessary for your requirements (which you apparently haven't stated fully, so I can't address).
curtranhome



Joined: 04 Jun 2008
Posts: 77

PostPosted: Wed Dec 16, 2009 11:26 pm     RE: Reply with quote

Ok well, when I insert and upload the file with only the function, it works, but when I add the echo to the absolute end of the php script, it dumps the entire page. So I want to know how the script is put together (noteing that you have them in seperate code area)
kanenas



Joined: 14 Dec 2004
Posts: 341

PostPosted: Thu Dec 17, 2009 7:47 pm     Reply with quote

What do you mean by "dumps the entire page"?

I had them separate because fortnightly was a particular implementation and the echo statement was a usage example. You'd integrate it into your page however you wish (generally speaking, group functions by related tasks into scripts, then include the scripts and call the functions in the pages). I have no idea how your pages are coded, thus have no idea how to add fortnightly into your pages. This is why I included an example of how to use the fortnightly function.
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum -> PHP All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
DARFUR
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial