PHP Classes

File: format_timestamp_example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Olaf Lederer  >  Format timestamp  >  format_timestamp_example.php  >  Download  
File: format_timestamp_example.php
Role: Example script
Content type: text/plain
Description: Example of using/testing
Class: Format timestamp
Convert date and time between UNIX and ISO formats
Author: By
Last change:
Date: 10 years ago
Size: 2,886 bytes


Class file image Download
$my_time = new format_timestamp;

if (isset(
$_POST['sub_date'])) {
$my_time->reg_date = $_POST['date'];
$my_time->reg_time = $_POST['time'];
// this property will output an other seperator then a minus
$my_time->date_seperator = ".";
// use this property if you have a not english styled date
$my_time->date_format = (isset($_POST['date_type'])) ? $_POST['date_type'] : "us";
// next convert the date
// the next method will create the mysql timestamp
if ($my_time->create_mysql_timestamp()) {
$mysql = $my_time->mysql_timestamp;
// after this you can convert this to an unix stamp
$unix = $my_time->conv_to_unix_timestamp();

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example format timestamp class</title>
<style type="text/css">
label {
    width: 100px;
input {
form {
#main {
    margin:0 auto;

<body style="text-align:center;">
<div id="main">
  <h2>Example date/time to MySQL and UNIX timestamp conversion</h2>
  <p>Some people have problems with the conversion from a date posted by a form and into a format for later calculations or just putting the value into a database.<br>
  With this class is it possible to convert a date and time field into a (MySQL or UNIX) timestamp. Of course you can extract date and time from a timestamp, too. Before conversting there is a test to check that a date/time is valid.</p>
  <p>Try it, insert a date and time and notice both timestamps.<br>The format for a non english date is: <b>dd-mm-yyyy</b></p>
  <form action="<?php echo $PHP_SELF; ?>" method="post">
    <label for="date">Date:</label>
    <input type="text" name="date" value="<?php echo (isset($_POST['date'])) ? $_POST['date'] : "yyyy-mm-dd"; ?>" size="15"><br>
    <label for="time">Time:</label>
    <input type="text" name="time" value="<?php echo (isset($_POST['time'])) ? $_POST['time'] : "hh:mm:ss"; ?>" size="15"><br>
    non english date?*
    <input type="checkbox" name="date_type" value="eu"<?php echo (isset($_POST['date_type'])) ? " checked" : ""; ?>><br>
    <input type="submit" name="sub_date" value="Convert!">
  <p><b><?php echo (isset($my_time->error)) ? $my_time->error : "&nbsp;"; ?></b></p>
  <?php if (isset($mysql) && isset($unix)) { ?>
<p>Your date time as MySQL timestamp: <b><?php echo $mysql; ?></b><br>and as UNIX timestamp: <b><?php echo $unix; ?></b></p>
  <p>Notice the extracted date with different seperators: <b><?php echo $my_time->extract_date(); ?></b></p>
  <?php } ?>