-3

Consider the following code.

    <html>
    <form method="POST">
    <input type="text" name="name1">
    <input type="date" name="date1">
    <input type="submit" name="sub" value="sub">
    </form>
    </html>

    <?php
    if(isset($_POST['sub']))
    {  
    echo $_POST['date1'];//line 11
    echo "<br>";
    echo $_POST['name1'];
    }
    ?>

line 11 display the date i have selected.But i want to echo the day,month,year separately. I am new to php. Help me please.Thanks in advance for your help.

sahira shamsu
  • 47
  • 2
  • 11

1 Answers1

0

From your code, it appears that your date should post back in the following format: yyyy-mm-dd

To split the date up into its individual elements, you need php's date() function.

First, however, you'll need to pass $_POST['date1'] through the strtotime() since date() wants a unix timestamps as a parameter.

$date_timestamp = strtotime($_POST['date1']);

Once you've got your input date converted to a timestamp, you can use date() to extract elements (year, month, day).

$day = date("d", $date_timestamp);
$month = date("m", $date_timestamp);
$year = date("Y", $date_timestamp);

Read more on date() and strtotime() to see different output formats.

nageeb
  • 2,002
  • 1
  • 13
  • 25