简体   繁体   中英

How to do auto convert for php date format?

I have recently got a PHP coding for a simple calender pickup. But it is in date format of mm/dd/yy. Is there anyway that I can auto convert it to yy-mm-dd? I heard using strtotime can change it automatically when it enters the database but I do not know how and where to add the code to. I try adding like.. strftime("%Y-%m-%d", strtotime($Date)) in the main page but I do not know where to add this but all the places are not valid. Any help to offer? Help is greatly appreciated.

This is roughly how it looks like in my main page:

if(isset($_POST["insert_click"]))
{
    $Date=$_POST["Date"];
    strftime("%Y-%m-%d", strtotime($Date)); //I tried putting it here but it does not work
    $Time=$_POST["Time"];
    $query=$connect->prepare("insert into Booking(Date, Time) values (?,?)");
    $query->bind_param('ss', $Date, $Time);
    $query->execute();
}

This is roughly how it looks in my form page:

<tr>
<td>Date:</td>
<td>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery UI Datepicker - Format date</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
    $(function()
    {
        $( "#datepicker" ).datepicker({minDate:0});
        $( "#format" ).change(function()
        {
            $( "#datepicker" ).datepicker();
        });
    });
    </script>

</head>
<body>

<input type="text" id="datepicker" size="30" name="Date"/>

</body>
</html>
</td>
</tr>

The problem is that you need to save the results of the date conversion to a variable. This statement:

strftime("%Y-%m-%d", strtotime($Date)); 

will do the date conversion and return the converted date, but will leave $Date alone.

To fix this, you need to caputure the results by storing the results to a variable:

$Date = strftime("%y-%m-%d", strtotime($Date));

Also, since you want yy-mm-dd, use %y instead of %Y .

Your line strftime("%Y-%m-%d", strtotime($Date)); is correct. You have only forgotten to assign the return value of this function to a variable. You will want something like this:

$newDate = strftime("%Y-%m-%d", strtotime($Date));

If you don't need the original date anymore, you can also do:

$Date = strftime("%Y-%m-%d", strtotime($Date));

Note: These days, you can be pretty lazy when it comes to PHP time formats for use in database or HTML's <time datetime="bla"></time> tag. Both the "normal" and localized time formats give you a shortcut to generate commonly used time value combinations, examples are:

  1. c for date()
  2. %F for strftime()

一种选择是使用本机日期函数,如下所示:

$Date=DATE('y-m-d, strtotime($_POST["Date"]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM