Formatting time (perl)

From HalfgeekKB
Revision as of 15:45, 14 November 2005 by 161.253.47.104 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

MySQL format

This function returns a time formatted into the MySQL format, YYYY-MM-DD HH:mm:SS. The time may be passed as a Unix timestamp or as the result of gmtime or localtime.

sub form_time {
	if(@_ > 1) {
		my ($sec,$min,$hour,$mday,$mon,$year) = @_;
		++$mon;
		$year += 1900;
		return sprintf('%04d-%02d-%02d %02d:%02d:%02d',
			$year,$mon,$mday,$hour,$min,$sec);
	}
	elsif(@_ == 0) {
		return form_time(gmtime());
	}
	else {
		return form_time(gmtime($_[0]));
	}
}