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.
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.
Share Improve this question edited Jun 13, 2016 at 19:21 Ilyas karim 4,8124 gold badges34 silver badges48 bronze badges asked Feb 11, 2016 at 15:35 Besjan VeiziBesjan Veizi 11 gold badge1 silver badge4 bronze badges 7-
RTFM: php/date You didn't specify the second argument, so by default date() will use
time()
, which means you're generating a "now" date, always. – Marc B 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? – Panda Commented Feb 11, 2016 at 15:44
- because I just want to show by default the date of today – Besjan Veizi Commented Feb 11, 2016 at 15:45
- I want that every date I choose can be saved in a php format – Besjan Veizi Commented Feb 11, 2016 at 15:47
- the datepicker is extremly important – Besjan Veizi Commented Feb 11, 2016 at 15:48
3 Answers
Reset to default 1Submit 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";
}
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>
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/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.