diff options
Diffstat (limited to 'time.pinc')
-rw-r--r-- | time.pinc | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/time.pinc b/time.pinc new file mode 100644 index 0000000..aa5421b --- /dev/null +++ b/time.pinc @@ -0,0 +1,139 @@ +<?php +/* +** $Id$ +** +** time.pinc - Time routines for ISO/Unix/Human dates +** +** ITools - the Internet Tools Library +** +** Copyright (C) 1995-2003 by the ITools Authors. +** This program is free software; you can redistribute it and/or +** modify it under the terms of either the GNU General Public License +** or the GNU Lesser General Public License, as published by the Free +** Software Foundation. See http://www.gnu.org/licenses/ for details. +*/ + +/** + * Get the current date + * @see current_time, now + * @return Current date in ISO (aka mysql) format: YYYY-MM-DD + */ +function current_date() +{ + return date('Y-m-d'); +} + + +/** + * Get the current time + * @see current_date, now + * @return Current time in ISO (aka mysql) format: HH:MM:SS + */ +function current_time() +{ + return date('H:i:s'); +} + + +/** + * Get the current date and time + * @param $offset Optional offset in seconds (defaults to 0) + * @return Current date and time in ISO (aka mysql) format: YYYY-MM-DD HH:MM:SS + * @see current_date, current_time + */ +function now($offset=0) +{ + return date('Y-m-d H:i:s', time()+$offset); +} + + +/** + * Calculate the number of days since January 1, 1970 for a given ISO date + * @see mysqldatetoseconds + * @param $date Date in ISO format: YYYY-MM-DD + * @param $date Time in ISO format: HH:MM:SS + * @return The supplied date as number of day since the start of the epoch + */ +function mysqldatetounix($date, $time) +{ + $dfields = split('-', $date); + $tfields = split(':', $time); + $unixtime = intval(mktime($tfields[0], $tfields[1], $tfields[2], $dfields[1], $dfields[2], $dfields[0])); + /* echo "$date $time --> " . $dfields[0] . "-" . $dfields[1] . "-" . $dfields[2] . " " . $tfields[0] . ":" . $tfields[1] . ":" . $tfields[2] . " ($unixtime sec)<br>\n"; */ + return $unixtime / 86400; +} + + +/** + * Calculate the number of seconds since January 1, 1970 for a given ISO date + * WARNING: Works only for years less than about 2038 + * @see mysqldatetoseconds, it_date_mysqltimestamptounix + * @param $datetime Date/time in ISO (aka mysql) format: YYYY-MM-DD HH:MM:SS + * @return The supplied date as number of seconds since the start of the epoch + */ +function mysqldatetoseconds($datetime) +{ + $dt = split(' ', $datetime); + $dfields = split("-", $dt[0]); + $tfields = split(":", $dt[1]); + $unixtime = intval(mktime($tfields[0], $tfields[1], $tfields[2], $dfields[1], $dfields[2], $dfields[0])); + /* echo "$datetime --> " . $dfields[0] . "-" . $dfields[1] . "-" . $dfields[2] . " " . $tfields[0] . ":" . $tfields[1] . ":" . $tfields[2] . " ($unixtime)<br>\n"; */ + return $unixtime; +} + + +/** + * Calculate the number of seconds since January 1, 1970 for a given mysql timestamp + * (which is in format YYYYMMDDhhmmss). Ignores all non-numerical characters, so this + * function can be used as a substitute for mysqldatetoseconds(). + * WARNING: Works only for years less than about 2038 + * @see mysqldatetoseconds, it_date_unixtomysqltimestamp + * @param $ts Date/time in ISO (aka mysql) format: YYYYMMDDhhmmss + * @return The supplied date as number of seconds since the start of the epoch + */ +function it_date_mysqltimestamptounix($ts) +{ + $ts = ereg_replace('[^0-9]+', '', $ts); + return intval(mktime(substr($ts, 8, 2), substr($ts, 10, 2), substr($ts, 12, 2), substr($ts, 4, 2), substr($ts, 6, 2), substr($ts, 0, 4))); +} + + +/** + * Convert the given UNIX timestamp to a mysql timestamp in format YYYYMMDDhhmmss. + * WARNING: Works only for years less than about 2038 + * @see mysqldatetoseconds, it_date_mysqltimestamptounix + * @param $ts Date/time in UNIX format (number of seconds since January 1, 1970) + * @return The supplied date as YYYYMMDDhhmmss + */ +function it_date_unixtomysqltimestamp($ts) +{ + return strftime('%Y%m%d%H%M%S', $ts); +} + + +/** + * Return the supplied date/time in human-readable Swiss format + * WARNING: Works only for years less than about 2038 + * @see swisstimestamp, mysqldatetoseconds + * @param $datetime Date/time in ISO (aka mysql) format: YYYY-MM-DD HH:MM:SS + * @return The supplied date/time formatted as "14.08.1966 06:15" + */ +function swissdate($datetime) +{ + return strftime("%d.%m.%Y %H:%M", mysqldatetoseconds($datetime)); +} + + +/** + * Return the supplied UNIX timestamp in human-readable Swiss format + * WARNING: Works only for years less than about 2038 + * @see swissdate, mysqldatetoseconds + * @param $datetime Optional date/time in seconds from the beginning of the epoch (default is now) + * @return The supplied date/time formatted as "14.08.1966 06:15" + */ +function swisstimestamp($datetime = null) +{ + return strftime('%d.%m.%Y %H:%M', isset($datetime) ? $datetime : time()); +} + +?> |