HTML5输入类型日期:如何制作日期字符串

时间:2013-01-08 06:04:53

标签: php javascript html5

1i一直在使用html 5的输入类型=“日期”,发现它非常有用。我只是想知道这是否可能:

假设我在日期选择器中选择了1/01/2012

我想将它保存在我的数据库(mysql)上,但我希望它像这样“2012年1月1日”

有办法做到这一点以及如何做?

require_once "includes/database.php";

$title=$_POST['txtTitle'];
$old_date  = explode("/",$_POST['txtDate']);
$new_date = $old_date[1].' '.$old_date[0].','.$old_date[2];
$date=$new_date;
$from=$_POST['txtFrom'];
$to=$_POST['txtTo'];
$place=$_POST['txtPlace'];
$details=$_POST['txtDetails'];
$id=$_POST['txtEid'];   

        if($id==""){
            $sql = "INSERT INTO events (event_title,event_date,event_tfrom,event_tto,event_place,event_details) VALUES (:title,:date,:from,:to,:place,:details)";
            $qry = $db->prepare($sql);
            $qry->execute(array(':title'=>$title,':date'=>$date,':from'=>$from,':to'=>$to,':place'=>$place,':details'=>$details));
        }else{
        $sql = "UPDATE events SET event_title=?, event_date=?, event_tfrom=?, event_tto=?, event_place=?, event_details=? WHERE event_id=?";
            $qry = $db->prepare($sql);
            $qry->execute(array($title,$date,$from,$to,$place,$details,$id));
        }

    echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>";
    echo "<script language='javascript' type='text/javascript'>window.open('cms_events.php','_self')</script>";

3 个答案:

答案 0 :(得分:1)

使用javascript你可以做这样的事情

    <button onclick = "getDate()">click for date</button>
    <script>
    function getDate(){
       var monthsOfYear = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

   var d = new Date();
   var currdate = d.getDate();
   var currmonth = d.getMonth();
   var curryear = d.getFullYear();

   alert(monthsOfYear[currmonth] + " "+currdate+","+curryear);
}

希望这会有所帮助..

答案 1 :(得分:0)

你可以通过使用一些php函数爆炸来实现。

$old_date  = explode("-",$_POST['date']);

$new_date = $old_date[2].' '.$old_date[1].','.$old_date[0];

echo $new_date;

您将以2012年1月1日的格式获得

答案 2 :(得分:0)

这对你的代码来说非常合适,请检查它。

$old_date  = explode("-",$date);



$months = array('January'=>"01",'February'=> "02", 'March'=>"03",'April'=>"04",  'May'=>"05", 'June'=>"06", 'July'=>"07", 'August'=>"08",'September'=> "09",'October'=>"10", 'November'=>"11", 'December'=>"12");


$nw_month = array_search($old_date[1],$months);


$new_date = $old_date[2].' '.$nw_month.','.$old_date[0];

echo $new_date;