Perl date modification
Hi,
I have a great guestbook script, except for one thing. It uses this line to get the date:
$the_date=localtime();
localtime() outputs the date and time where the server is, in this format:
Thu Mar 8 12:49:43 2001
Does anyone know if/how I can change the format of the ouputted string e.g. print "8/3/01, 12:49" instead (for example)? And can you modify localtime() to print the time in your own time zone?
Thanks for anything in advance.
Mark Hensler posted this at 22:39 — 9th March 2001.
He has: 4,048 posts
Joined: Aug 2000
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$year = substr($year,1); #make $year two digit
$the_date = "$mon/$mday/$year, $hour:$min";
interesting links:
http://www.perl.com/pub/doc/manual/html/pod/perlfunc/gmtime.html
Mark Hensler
If there is no answer on Google, then there is no question.
japhy posted this at 14:26 — 10th March 2001.
They have: 161 posts
Joined: Dec 1999
You can get the arguments of localtime() or gmtime() in a list (as the documentation states) and play with them as you need.
my ($min,$hour,$day,$mon,$year) = (localtime)[1..5];
my $datestr = sprintf "%02d/%02d/%02d, %02d:%02d", $day, $mon+1, $year % 100, $hour, $min;
You can also use the POSIX strftime function:
use POSIX 'strftime';
my $datestr = strftime('%d/%m/%y, %R', localtime);
Perl should be doing it for your specific time zone when you use localtime().
ronniec posted this at 21:00 — 10th March 2001.
They have: 5 posts
Joined: Mar 2001
Thanks to you both for the help.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.