0

1i 一直在使用 html 5 的 input type="date" 并发现它非常有用。我只是想知道这是否可能:

假设我在日期选择器中选择了 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>";
4

3 回答 3

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);
}

希望这可以帮助..

于 2013-01-08T06:24:32.953 回答
0

你可以通过使用一些 php 函数来做到这一点。

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

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

echo $new_date;

您将在 2012 年 1 月 1 日获得这种格式

于 2013-01-08T06:14:51.990 回答
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;
于 2013-01-08T07:27:54.427 回答