Forum Home
Press F1
 
Thread ID: 18109 2002-04-18 00:33:00 Help with fixing date problem in CGI/Perl Guest (0) Press F1
Post ID Timestamp Content User
44372 2002-04-18 00:33:00 Here's the script:

sub get_date {
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June' ,'July',
'August','September','October','November','Decembe r');
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf('%02d:%02d:%02d',$hour + 16,$min,$sec);
$year += 1900;
$date = '$days[$wday], $months[$mon] $mday, $year at $time';

I have added + 16 to the hours because this is how much the server is off by but this didn't work quite as intended as it now displays times above 24 hours.

I have no experience in CGI/Perl and have found no information regarding this. Is there a way to say if hours is greater than 24 then minus 24 and add one day? Don't know if that will work but if there's a way to do it could you please help me out.

TIA
Doc Flash
Guest (0)
44373 2002-04-18 03:06:00 My Perl isn't that great but you could try:

sub get_date {
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June' ,'July',
'August','September','October','November','Decembe r');
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf('%02d:%02d:%02d',$hour + 16,$min,$sec);
if ($hour>=24)
{
$hours - 24 && $mon || $wday + 1
}
$year += 1900;
$date = '$days[$wday], $months[$mon] $mday, $year at $time';

I am sure it maybe incorrect but it's basically what you are asking it to do.

Cheers.
Guest (0)
44374 2002-04-18 05:17:00 I had a look over that script and did a lot of working out with it and I know Dunno Aye's script didn't work and it kind of confusing but this might work.

Too bad it's 4 pm though as you can't test it till 12 am.

Anyway here it is. It maybe wrong as I didn't test it properly.

sub get_date {
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June' ,'July',
'August','September','October','November','Decembe r');
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
if ($hour + 16 >= 24)
{
$hour = ($hours - 24);
$mday = ($mday + 1);
$wday = ($wday + 1);
}
$time = sprintf('%02d:%02d:%02d',$hour + 16,$min,$sec);
$year += 1900;
$date = '$days[$wday], $months[$mon] $mday, $year at $time';
Guest (0)
44375 2002-04-18 15:16:00 OK so I thought it was correct but I changed my time back and tested it out and what did you know... It wasn't correct but I think I've got the answer to it now. Isn't programming fun?

Anyway change

if ($hour + 16 >= 24)
{
$hour = ($hours - 24);
$mday = ($mday + 1);
$wday = ($wday + 1);
}

with

if ($hour <= 7)
{
$hour = $hour + 16
}
elsif ($hour >= 8)
{
$hour = ($hour - 8);
$mday = ($mday + 1);
$wday = ($wday + 1);
}
Guest (0)
44376 2002-04-18 15:22:00 Left out another bit.

Where you've got $hour + 16 get rid of it and just have $hour

(not the one in the fix I gave)
Guest (0)
44377 2002-04-23 07:41:00 I had the same program with one of my CGI scripts not working properly because of the server time being 19 hours behind. I sweated over this for a long time, until I realised that there is a really simple solution. Where you have =(localtime(time)), add +57600 straight after time, e.g. =(localtime(time+57600)). This simply adds 16 hours to the current time returned by the server (16*60*60=57600). Hope this helps, and happy programming :-) Guest (0)
44378 2002-04-23 07:45:00 PS. the first line is supposed to say 'I had the same PROBLEM with one of my CGI scripts'
Cheers, MM.
Guest (0)
1