0

I have created a planner using php and jquery on my application. I have a section, where the user inputs start date and start time, and another where they input the end date and end time. The fields are all seperate, like so:

<input type="text" name="dateFrom" id="dateFrom">  <!-- datepicker plugin -->
<input type="text" name="time_from" id="timeFrom">  <!-- timepicker plugin -->

<input type="text" name="dateTo" id="dateTo">  <!-- datepicker plugin -->
<input type="text" name="time_to" id="timeTo">  <!-- timepicker plugin -->

What I want to know is once the user has inputted the above information, and submits the data to the database, instead of creating seperate columns in the table, is there anyway of inserting the dateFrom and timeFrom fields into one database column named start_time as a UNIX timestamp, and also the same for the dateTo & timeTo fields.

Can anyone give me any ideas on how I would do this?

4

2 回答 2

0

就像是

INSERT INTO yourtable (start_time, end_time)
VALUES (UNIX_TIMESTAMP('$dateFrom $timeFrom'), UNIX_TIMESTAMP('$dateTo $timeTo'))

会做,假设你的 $dateFrom 是yyyy-mm-dd,并且 $timeFrom 是hh:mm:ss。当然,如果您直接将表单字段填充到查询中,这将受到 SQL 注入攻击,但这只是向您展示您应该执行此操作的一种方法。

于 2013-05-13T15:08:11.333 回答
0

You can combine the date and time using strtotime to get a UNIX timestamp before the insertion.

$dateFrom = "2013-05-13";
$timeFrom = "13:52";

$fromStamp = strtotime("$dateFrom $timeFrom");

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

于 2013-05-13T15:05:41.250 回答