Gossamer Forum
Home : Products : DBMan SQL : Discussion :

extracting parts of datetime field

Quote Reply
extracting parts of datetime field
Hi,
I have a datetime field <%Launched%> which is storing data in the form 2003-06-17 12:00:00.
How can I check in a template (or must be with a global?) the sole value of one part of <%Launched%> content for a conditional branch. For example: if the time is 00:00:00 then...., or if the month is 07 then.... I have read the GT date help but cann't figure it out. Thanks for any assistance
Charly
Quote Reply
Re: [charly] extracting parts of datetime field In reply to
Just simply create a global template like:

format_date => '

sub {
my ($value, $format) = @_;
return if (!$value);
$format ||= '%yyyy%-%mm%-%dd%';

require GT::Date;
return GT::Date::date_get($value, $format);
}

'

Now, you can use it in a template:
<%set time_formated = format_date($Launched, '%hh%:%MM%:%ss%')%>
<%if time_formated eq '00:00:00'%>
do something
<%endif%>

or:
<%set month_formated = format_date($Launched, '%mm%') %>
<%if month_formated eq '07'%>
do something
<%endif%>

Hope that helps,

TheSTone.

B.
Quote Reply
Re: [TheStone] extracting parts of datetime field In reply to
Something is amiss with your global/template suggestion and that as from the format_date operation even before the conditional testing.

My field contains 2003-06-19 00:00:00
If I apply the line <%set time_formated = format_date($Launched, '%hh%:%MM%:%ss%')%>
and print out the value <%time_formated%> I obtain 07:33:33 (time somewhere I presume!)
Same thing for the month_formated which gives me 12 instead of 6.
I have tried reverting the field to a straight date field. No difference.
More time ;) on this one please!
Charly
Quote Reply
Re: [charly] extracting parts of datetime field In reply to
oops...can you fix the global a little bit:

sub {
my ($value, $format) = @_;
return if (!$value);
$format ||= '%yyyy%-%mm%-%dd%';
require GT::Date;
my $v = GT::Date::timelocal(GT::Date::parse_format($value, "%yyyy%-%mm%-%dd% %hh%:%MM%:%ss%"));
return GT::Date::date_get($v, $format);
}

It should work.

TheStone.

B.
Quote Reply
Re: [TheStone] extracting parts of datetime field In reply to
I just changed the formatting to 24 clock notation with %HH% instead of %hh% and it works great
Thanks, perfect this time ;)