0

1i have been using html 5's input type="date" and found it very useful. i just wanna know if this is possible:

suppose i chose 1/01/2012 in the date picker

i want to save it on my database (mysql) but i want it to be like this " January 1,2012 "

are there ways to do it and how?

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>";
2
  • 2
    Why not save it in the date's default format (YYYY-MM-DD) and then alter the date when it is read from the database? The date will be in a generic mSQL DATE format and thus can have its format easily altered throughout you application. Commented Jan 8, 2013 at 6:08
  • i.imgur.com/kj3c1.png?1 i want to make it like what in that picture shows. but all the date were coming from my database and the date saves in int values. Commented Jan 8, 2013 at 6:13

3 Answers 3

1

Using javascript you can do something like this

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

Hope this helps..

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

Comments

0

You can do that by using some php functions explode.

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

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

echo $new_date;

You will get in this format January 1,2012

13 Comments

I don't think you will get January. You will get 1 instead.
$old_date = explode("/",$_POST['txtDate']); $new_date = $old_date[1].' '.$old_date[0].','.$old_date[1]; $date=$new_date; i am now getting an error: Notice: Undefined offset: 1 in C:\xampp\htdocs\pcf\saveevent.php on line 6
@Hussain Yes you wont because you have to change the month from 'm' to 'M' , so that January is displayed instead 01, February displayed instead of 02 ..etc
@JB De Asis can you display your complete code so that i can debug it??
Dude Just check your code and also whether $_POST['txtDate'] is working for you....The code i hav written is working perfectly for me though
|
0

This wil work perfectly fine for your code, check it..

$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;

1 Comment

@JB De Asis try the above code, you wil get your desired result

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.