Gossamer Forum
Home : Products : DBMan SQL : Development, Plugins and Globals :

Current Date plus One Month

Quote Reply
Current Date plus One Month
I have a global named "current_date" as follows -
sub {
my ($date, $format) = @_;
require GT::Date;
my $unix_time = GT::Date::timelocal(GT::Date::parse_format($date, "%yyyy%-%mm%-%dd% %hh%:%MM%:%ss%"));
return GT::Date::date_get($unix_time, $format);
}

In my html template I have the following -
<%current_date($var_date, "%mm%")%>

This returns the current date in two digit format (e.g the month of May is returned as 05).
I put the above code together from searching various GT posts, so don't know if it's the correct way to achieve the result but it seems to work. I also use the other parts of the date (year, day, time, etc.) in another template.

Now, what I want to do is display the next month (e.g plus one month. So if it's May, I want to display 06 for June).

How can I do that from within the html template?
I tried -
<%current_date($var_date, "%mm%") + 1%>
but it doesn't do the job.
Any ideas?
Thank you.
Quote Reply
Re: [jai] Current Date plus One Month In reply to
   
Hi jai

try to modify your "current_date" global as follows:

Code:

sub {
my ($date, $format, $add_days) = @_;
require GT::Date;

use GT::Date qw /:all/;
my $unix_time = GT::Date::timelocal(GT::Date::parse_format($date, "%yyyy%-%mm%-%dd% %hh%:%MM%:%ss%"));

$unix_time_added = date_add($unix_time, $add_days);
return GT::Date::date_get($unix_time_added, $format);
}




how to call it in your HTML template:

Code:

<%current_date($var_date, "%mm%", "30")%>


You can add as many days as you like - I've used 30 for one month.

Let me know if it works...

Cheers,
Oliver
Quote Reply
Re: [olivers] Current Date plus One Month In reply to
Hi Oliver,
Thank you for your reply.
I've been playing around with the code you supplied but for some reason it's not giving me the correct current date or calculating the new date. I had to declare one of your variables (in red) to get the code to run but then it's giving me a current date of 1969 for the year and 12 for the month. I think the problem is in the sub and not my template (as changing the code in the template makes no difference to what is displayed).
Do you have any ideas as to what might be causing this?
Thank you.

sub {
my ($date, $format, $add_days) = @_;
require GT::Date;
use GT::Date qw /:all/;
my $unix_time = GT::Date::timelocal(GT::Date::parse_format($date, "%yyyy%-%mm%-%dd% %hh%:%MM%:%ss%"));
my $unix_time_added = date_add($unix_time, $add_days);
return GT::Date::date_get($unix_time_added, $format);
}

Quote Reply
Re: [jai] Current Date plus One Month In reply to
Sorry about that and thanks for your feedback Blush

let's start another try:

Code:

sub {
my ($date, $format, $add_days) = @_;
require GT::Date;
use GT::Date qw /:all/;
my $date_added = date_add($date, $add_days);
my $unix_time = GT::Date::timelocal(GT::Date::parse_format($date_added, "%yyyy%-%mm%-%dd% %hh%:%MM%:%ss%"));
return GT::Date::date_get($unix_time, $format);
}


I hope it works this time Wink

All the best
Oliver
Quote Reply
Re: [olivers] Current Date plus One Month In reply to
Thanks again Oliver,
The current date is now correct but the new date is not working.
I have the following code in my html template but it is not adding the 30 days.
<%current_date($var_date, "%mm%", "30")%>

Thank you for your time.
Simon.

Quote Reply
Re: [jai] Current Date plus One Month In reply to
Let me explain you how it works on my system:

this is my globel "date_add":

Code:

sub {
my ($datefield, $add_days) = @_;
require GT::Date;
use GT::Date qw /:all/;
my $date_added = date_add($datefield, $add_days);

return $date_added;
}


This is how I call it in the HTML template:

Code:

<%date_add($nameofmydatefield,'%mm%','30')%>


For example: if you have a db field called "specialdate", you could put <%date_add($specialdate,'%mm%','30')%> and this would be the calculated output (date in specialdate field plus 30 days). I don't know what you exactly want to do with the timelocal thingy in your sub. And I'm also wondering what kind of date field type you have defined in MySQL.

Thanks for more info
Oliver
Quote Reply
Re: [olivers] Current Date plus One Month In reply to
Hi Oliver,
I guess I should have explained what I am trying to do in a little more detail.
I'm not trying to format the date for insertion into my database. What I am trying to do is format some dates to use in search links in my html template -
<a href="db.cgi?db=dbname&do=search_results&year=<%current_year%>&month_id=<%current_month%>">Current</a> |
<a href="db.cgi?db=dbname&do=search_results&year=<%next_year%>&month_id=<%next_month%>">Next Month</a>

Anyway, what I did was make a new sub as follows -

sub {
my $tags = shift;
use GT::Date qw/:all/;
#set the month format
date_set_format('%mm%');
#get the current month
$tags->{current_month}=date_get ();
#get next month
$tags->{next_month}=date_get (time + (30 * 86400));
#set the year format
date_set_format('%yyyy%');
#get the current year
$tags->{current_year}=date_get ();
#get the year plus 30 days
$tags->{next_year}=date_get (time + (30 * 86400));
return;
}

It seems to do what I want.
I couldn't get it work with the method that you suggested (it kept giving me strange dates and wasn't calculating the new dates).

Thank you.
Quote Reply
Re: [jai] Current Date plus One Month In reply to
Hi jai,

many thanks for your feedback - now I understand that you would like to use the code for search links. It's an interesting idea and I will try it out on my machine.

My global is doing "calculated" date output on the search result page. There is a section in my template where the value of a date field + 30 days is being displayed.

It seems that we have solved a similar problem for different purposes Smile

All the best,
Oliver