• Date/time to MySQL and UNIX timestamp conversion using PHP

    Below steps will describe how to convert date/time to mysql and unix timestamp using PHP.

    Step 1 : 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.
    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. Create new php file and insert below code in it. Save this file with name format_timestamp_class.php.

    1
    
     

    Step 2 : Now, create another php file and insert below code in it. Save that file with ‘format_timestamp_example.php’.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    <!--?php 
    include("format_timestamp_class.php");
    error_reporting(E_ALL);
    $my_time = new format_timestamp;
     
    if (isset($_POST['sub_date'])) {
        $my_time--->reg_date = $_POST['date'];
        $my_time-&gt;reg_time = $_POST['time'];
        //  this property will output an other seperator then a minus
        $my_time-&gt;date_seperator = "."; 
        // use this property if you have a not english styled date
        $my_time-&gt;date_format = (isset($_POST['date_type'])) ? $_POST['date_type'] : "us"; 
        // next convert the date
        $my_time-&gt;conv_date_format();
        // the next method will create the mysql timestamp
        if ($my_time-&gt;create_mysql_timestamp()) {
            $mysql = $my_time-&gt;mysql_timestamp;
            // after this you can convert this to an unix stamp
            $unix = $my_time-&gt;conv_to_unix_timestamp();
        } 
    }
     
    ?&gt;<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    Try it, insert a date and time and notice both timestamps.
    The format for a non english date is: dd-mm-yyyy

    ” size=”15″>
    ” size=”15″>
    non english date?* >

     

    Your date time as MySQL timestamp:
    and as UNIX timestamp:

    Notice the extracted date with different seperators:extract_date(); ?>

    Step 3 : Now, run the file ‘format_timestamp_example.php’, you will get the output where you can convert date/time to mysql and unix timestamp.

Comments on this post

Leave a Reply

  • Security Code :


    5 + four =