English 中文(简体)
• 如何使用户对时间和日期作出正常投入,使之在我sql作为时间序列添加?
原标题:How to make a normal user input of time and date to get it added in mysql as timestamp?
  • 时间:2011-11-22 05:30:06
  •  标签:
  • php
  • mysql

在用户接口中,即有一个文本领域以dd/mm/yyy格式进入日期,还有另一个文本箱,时间为h:mm格式和另一个滚落箱,以具体说明距离或时间。 所有这三个领域都将让用户人工进入日期、时间及时间。 我倾向于按照项目的要求使用这一原始方法。

现在,我想要在时间规划格式中以我sql补充所有这些价值观,使之更易于 f和操纵。 请建议我如何做到这一点。

<?
$date =  21 . / . 11 . / . 2011 ;
$time =  12 . : . 00 ;
$ampm = "am";
$dt = DateTime::createFromFormat(
     d/m/Y h:i a ,
 sprintf( %s %s %s , $date, $time, $ampm),
   new DateTimeZone( Country/Region ));



$mysqlDateString = $dt->format( Y-m-d H:i:s );


?>
最佳回答

rel=“nofollow”>mktime,加上正确的格式 通过以下方式向explode发送用户投入:

$date =  22/11/2011 ; $date_pieces = explode(  / , $date);
$time =  08:35 ; $time_pieces = explode(  : , $time);
$am_pm =  PM ;

$mysql_timestamp = date(  Y-m-d H:i:s , mktime( 
    $time_pieces[0] + ( $am_pm ==  AM  ? -12 : 12), // Hour - Convert AM/PM to 24-hr format
    $time_pieces[1], // Minute
    0, // Second
    $date_pieces[1], // Month
    $date_pieces[0], // Day
    $date_pieces[2])); // year

现在$mysql_timestamp是一日记的有效条目。

如果您需要一个UNIX的时间序列,就删除了date的要求。

$unix_timestamp = mktime( 
    $time_pieces[0] + ( $am_pm ==  AM  ? -12 : 12), // Hour - Convert AM/PM to 24-hr format
    $time_pieces[1], // Minute
    0, // Second
    $date_pieces[1], // Month
    $date_pieces[0], // Day
    $date_pieces[2]);

Demo

<><>Edit>: 根据下文的哲学评论,可以确定一个正确的时区,网址是:date_default_timezone_set

问题回答

http://www.php.net/manual/en/datetime.create fromformat.php"rel=“nofollow”>:

假设形式领域已经投入使用,基本上验证并收集成变量......

// assume $date, $time, $ampm

$dt = DateTime::createFromFormat(
     d/m/Y h:i a ,
    sprintf( %s %s %s , $date, $time, $ampm),
    new DateTimeZone( Country/Region ));

$unixTimestamp = $dt->getTimestamp();

$mysqlDateString = $dt->format( Y-m-d H:i:s );

如果你将日期/时间重新定性为MySQL的日/时间/时间范围,你可能也要储存时间区,否则就不可能检索准确的数据。

There is loads of information over at MySQL about time conversions. In your case I think str_to_date would work.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签