 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
curtranhome

Joined: 04 Jun 2008 Posts: 77
|
Posted: Tue Nov 17, 2009 9:22 pm Every other week date |
|
|
|
| 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
|
Posted: Thu Dec 10, 2009 8:32 pm Need more info |
|
|
|
| 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
|
Posted: Thu Dec 10, 2009 9:58 pm RE: |
|
|
|
| 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
|
Posted: Fri Dec 11, 2009 7:22 pm Need more info |
|
|
|
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
|
Posted: Sat Dec 12, 2009 8:46 pm RE: |
|
|
|
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
Thanks ahead of time
~curtranhome |
|
kanenas

Joined: 14 Dec 2004 Posts: 341
|
Posted: Mon Dec 14, 2009 7:20 am Use rounding functions |
|
|
|
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
|
Posted: Mon Dec 14, 2009 2:41 pm RE: |
|
|
|
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
|
Posted: Wed Dec 16, 2009 9:46 pm What do you mean? |
|
|
|
| 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
|
Posted: Wed Dec 16, 2009 11:26 pm RE: |
|
|
|
| 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
|
Posted: Thu Dec 17, 2009 7:47 pm |
|
|
|
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. |
|
|
|
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
|
|
|
|
|