Location: PHPKode > scripts > Format timestamp > format-timestamp/format_timestamp_class.php
<?php
/************************************************************************
Format timestamp - v1.00
Convert date & time to UNIX and MySQL timeststamps

Copyright (C) 2004 - Olaf Lederer

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

_________________________________________________________________________
available at http://www.finalwebsites.com 
Comments & suggestions: http://www.finalwebsites.com/contact.php

_________________________________________________________________________
Changes/Updates:


*************************************************************************/
class format_timestamp {

	var $mysql_timestamp;
	var $unix_timestamp;
	var $reg_date;
	var $reg_time;
	var $date_seperator = "-";
	var $date_format = "us";
	var $formatted_date;
	
	// constructor to use this class with "current" values
	// plus call the "conv_to_unix_timestamp" to get a timestamp
	// for using date or time methods
	function format_timestamp() { 
		$this->reg_date = date("Y-m-d");
		$this->reg_date = "00:00:00";
	}
	// this method is used if you got only a date (or time)
	// a mysql_timestamp is created with only one variable
	function create_mysql_timestamp() {
		if (ereg("^(19|20)[0-9]{2}[-:,./[:space:]](0[1-9]|1[0-2])[-:,./[:space:]](0[1-9]|[1-2][0-9]|3[0-1])$", $this->formatted_date)) {
			$date = ereg_replace("[-.,/[:space:]]", "", $this->formatted_date);
		} else {
			if ($this->date_format == "eu") {
				$this->error = "The date is not recognized, use this format: dd-mm-yyyy";
			} else {
				$this->error = "The date is not recognized, use this format: yyyy-mm-dd";
			}
		}
		if (isset($date)) {
			if (ereg("^(([0]*[1-9])|(1[0-9])|(2[0-3]))[-:,./[:space:]]([0-5][0-9])[-:,./[:space:]]([0-5][0-9])$", $this->reg_time)) {
				$time = ereg_replace("[-:,./[:space:]]", "", $this->reg_time);
			} else {
				$this->error = "The time is not recognized, use this format: hh:mm:ss";
			}
			if (isset($time)) {
				$this->mysql_timestamp = $date.$time;
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	function conv_date_format() {
		if ($this->date_format == "eu") {
			$date_parts = split("-", $this->reg_date);
			$this->formatted_date = sprintf("%s-%s-%s", $date_parts[2], $date_parts[1], $date_parts[0]);
		} else {
			$this->formatted_date = $this->reg_date;
		}
	} 
	function conv_to_mysql_timestamp() {
		$this->mysql_timestamp = date("YmdHis", $this->unix_timestamp);
		return $this->mysql_timestamp;
	}
	function conv_to_unix_timestamp() {
		$hour = substr($this->mysql_timestamp, 8, 2)*1;
		$min = substr($this->mysql_timestamp, 10, 2)*1;
		$sec = substr($this->mysql_timestamp, 12, 2)*1;
		$month = substr($this->mysql_timestamp, 4, 2)*1;
		$day = substr($this->mysql_timestamp, 6, 2)*1;
		$year = substr($this->mysql_timestamp, 0, 4);
		$this->unix_timestamp = mktime($hour,$min,$sec,$month,$day,$year);
		return $this->unix_timestamp;
	}
	// method to get a date from a unix-timestamp
	// use this only to show a date
	function extract_date() {
		$this->conv_to_unix_timestamp();
		if ($this->date_format == "eu")  {
			$order = "d-m-Y";
		} else {
			$order = "Y-m-d";
		}				
		$tmp_date_only = date($order, $this->unix_timestamp);
		$date_only = str_replace("-", $this->date_seperator, $tmp_date_only);
		return $date_only;
	}
	function extract_time() {
		$this->conv_to_unix_timestamp();
		$time_only = date("H:i:s", $this->unix_timestamp);
		return $time_only;
	}

}		
?>
Return current item: Format timestamp