0

I want to "save" the value of into a php variable, so if in the "datebox" there is something like 11/02/2016, I want to save this value on $date, then I can do echo $date; and see again 11/02/2016. the datepicker is important

I tried on two ways:

1.

<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <!-- Today date -->
    <input type="date" name="today" value="<?php echo date("Y-m-d");?>">
</form>
</body>

<?php
    $date = date('Y-m-d', strtotime($_POST['today']));
    echo "Today is $date";
?>

This output: Today is 1970-01-01

2.

<head>
<script>
function myFunction() {
    var date = document.getElementById("today").value;
    document.getElementById("here").innerHTML = date;
}
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="date" name="today" value="<?php echo date("Y-m-d");?>">
    <input type="hidden" id="here" name="this">
</form>
</body>

<?php

    $data = $_POST['this']));
    echo "Today is $data";

?>

This output: Today is

Can anybody help me please?

Thanks in advance.

7
  • RTFM: php.net/date You didn't specify the second argument, so by default date() will use time(), which means you're generating a "now" date, always. Commented Feb 11, 2016 at 15:39
  • @BesjanVeizi What do you want to achieve? Do you want to format your date in dd/mm/yy or do you want the date to always be today? Commented Feb 11, 2016 at 15:44
  • because I just want to show by default the date of today Commented Feb 11, 2016 at 15:45
  • I want that every date I choose can be saved in a php format Commented Feb 11, 2016 at 15:47
  • the datepicker is extremly important Commented Feb 11, 2016 at 15:48

3 Answers 3

1

Submit Element is missing from your form which means form is not processing anything.So put submit button to process the form.Try this code.

<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="date" name="today" value="<?php echo date("Y-m-d");?>">
<input type="submit" name="a">
</form>
</body>
if(isset($_POST['a']))
{
  $date = date('Y-m-d', strtotime($_POST['today']));
  echo "Today is $date";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You don't have a submit element, and you have a coercion to null / 0 in your output. The result of the first is that you never submit anything. So your form doesn't actually process anything, a form does nothing until it's actually submitted.

Your second error results in the code always outputting the Unix Epoch, which is what a timestamp with an int result of 0 is. You need to turn on your errors for whatever server you're using. Turning off errors or suppressing them seems like a good idea, but it causes a host of easy-to-fix problems to remain undiagnosed.

This code should work on PHP 5.4 and above.

<?php
     $self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '#';
     $now = date("Y-m-d");
     $today = isset($_POST['today']) ? $_POST['today'] : (new DateTime)->format('Y-m-d');
     $date = date('Y-m-d', strtotime($today));
     $formattedResult = "Today is $date";
?>
    <form action="<?= $self; ?>" method="POST">

    <input type="date" name="today" value="<?= $today;?>">
    <button type='submit' >
        You must submit the form to process it
        </button>
</form>
<?= $formattedResult;?>
</body>

Comments

0

Using the built in function DateTime you can do

date_default_timezone_set("Europe/London"); //Set your timezone
$dateNow = new DateTime;

echo $dateNow->format('d/m/Y'); // Will echo 11/02/2016

http://php.net/manual/en/timezones.php for a list of supported timezones

EDIT: Including your HTML:

<?php 
    date_default_timezone_set("Europe/London"); //Set your timezone
    $date = new DateTime;
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <!-- today date-->
    <input type="date" name="today" value="<?php echo $date->format('Y-m-d'); ?>">
    <input type="submit" name="test" value="test">
</form>

Then later on:

<?php
    $datePOST = new DateTime($_POST['today']);
    echo $datePOST->format('d/m/Y');
?>

I hope this is what you were looking for.

2 Comments

I tried your code and it works only for the actual date (the last echo prints only the date of today). When I change date on the datapicker the echo doesn't update...any idea to update the output string?
I added the code, with a submit button on my website : themooliecommunity.com/Solo/test.php Can you check if that is what you need? I have also update the code to include the submit button

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.