I am a little stuck here how to write a code for current date of the server and validate them.
So basically on one file is the form.php and i have to write the code for current date of the server in dd/mm/yy and this can be edited by the user. The date should appear in a form format for example on the web browser should appear date: [23/9/2012].
and on the other file which is the process.php I have to validate the date.
so for my form.php this is what i wrote so far:
<html>
<body>
<?php
if (isset ($_POST["date"])){
$date = date("d/m/y"($_POST["date"]));
echo $date;
}
?>
<form action="form.php" method="POST">
Date: <input type="text" name="date" value="Date" />
</form>
</body>
</html>
what it appear on the web browser for date is only date:[date] just the word date but not the date. I've been stuck for an hour for this.
You can do this with a regex:
<?php
if (isset($_POST['date']) && !empty($_POST['date'])) {
$date = trim($_POST['date']);
}
$regex = '/^((([1-2][0-9])|([1-9]))/([2])/[0-9]{4})|((([1-2][0-9])|([1-9])|(3[0-1]))/((1[0-2])|([3-9])|([1]))/[0-9]{4})$/';
if (preg_match($regex, $date)) {
$disp_date = $date;
//update the date in db
$error = '';
}
else {
$error = 'Invalid date format<br />';
//get last date from db
$disp_date = $date_from_db;
}
?>
<?=$error?>
<form action="" method="POST">
<input type="text" value="<?=$disp_date?>" name="date" />
<input type="submit" value="Submit Date" />
</form>
How the regex works: DD/MM/YYYY: DD must be 1-31, MM 1-12 and YYYY a 4 digit num.
Edit: Fixed the problem with February.
<html>
<body>
<?php
if(isset($_POST['date']) && strtotime($_POST['date'])){ # date validation
$date = $_POST['date'];
} else {
$date = date('d/m/y');
}
?>
<form action="form.php" method="POST">
Date: <input type="text" name="date" value="<?php echo $date?>" />
</form>
</body>
</html>
you can get the current date and display like this...
<html>
<body>
<?php
$today = date("j, n, Y");
?>
<form action="form.php" method="POST">
Date: <input type="text" name="date" value="<?php echo $today?>"/>
</form>
</body>
</html>
and you can learn validate date from this link check date
You should rearrange your program like this:
<html>
<body>
<?php
if (!isset ($_POST["date"])){
$date = date("d/m/y");
else {
$date = $_POST["date"];
}
?>
<form action="form.php" method="POST">
Date: <input type="text" name="date" value="<?php echo $date; ?>" />
</form>
</body>
</html>
Here you will let the user see the current time server at the first page load, and the edited one afterwards.
try formatting it something like this
<?php
$date = new DateTime();
echo $date->getTimestamp();
echo $date->format('Y-m-d H:i:s');
?>