0

I have a form that looks like this:

  <td>Starts</td>
  <td><label for="mm"></label>
    <select name="mm" id="mm">
      <option value="">---</option>
      <option value="01">Jan</option>
      <option value="02">Feb</option>
    </select>
    <label for="dd"></label>
    <select name="dd" id="dd">
   <option value="">--</option>
       <option value="1">01</option>
       <option value="2">02</option>
    </select>
    <label for="yy">
      <input type="text" name="year" id="year" />
    </label></td>

It is for people to choose the month | day | year. But the field in the database is Unix timestamp format.

Any ideas how can I take this data and converted into a Unix timestamp format for my database?

6 Answers 6

3

Another alternative, in mysql:

INSERT ... UNIX_TIMESTAMP('$year-$month-$day') ...

once appropriate data sanitization and sql injection prevention's been taken care of.

Sign up to request clarification or add additional context in comments.

Comments

2

This'll help you, very easy to follow.

php strotime();

Comments

1
<?php
if ($_POST) {
    // convert POST to MySQL date format
    $date_string = sprintf('%04d-%02d-%02d', $_POST['year'], $_POST['mm'], $_POST['dd']);
    // if you need it in a UNIX timestamp, do the following
    $unix_timestamp = strtotime($date_string);
}

2 Comments

You've got months going in twice (mm).
Cheers. Fixed now. It's been a long day!
0
<?php

$time = new mktime($hour, $minute, $second, $month, $day, $year);

Or ..

<?php

$time = new DateTime();
$time->setDate($year, $month, $day);
// set both to 0, otherwise you will get the current time at the given day/month
$time->setTime($hour, $minute); 

There's a ton of ways to do this in php, spend some time looking over the date/time functions

Comments

0

If the form fields are captured variables $mm, $dd, $yy, then

$timestemp = strtotime($yy . '-' . $mm . '-' . $dd);

will indeed give you an integer $timestamp (in epoch seconds), as Grigor correctly points out. But in general that may be only half the battle. In your case the Unix timestamp format makes things simple. But depending on your DB (and your associated drivers and DB abstraction layer) you may in some cases need to convert this into a date string of some form. For example, Oracle takes date strings of the form YYYY-MM-DD HH:MM:SS, which would be generated by

$oracle_date_string = gmdate("Y-m-d H:i:s", $timestamp);

Comments

0

After you have posted the form back to php, you will end up with your form data in the special global $_POST. If your user has selected a date completely, the code snippet set $unix_ts to the unix timestamp of the date.

<?php

  $unix_ts = null;

  // validate date
  if (is_numeric($_POST['dd']) || is_numeric($_POST['mm']) || is_numeric($_POST['year'])) {
    if (($tmp = strtotime($_POST['mm']."/".$_POST['dd']."/".$_POST['year'])) !== false) {  
       $unix_ts = $tmp;
    }
  } else {
    // display error message however you choose
    echo "Date not set<br>";
  }

  if ($unix_ts != null) { 
     // do query
  }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.