Here's the code
input type="date" name="date" min="2016-12-28"
How can I auto increase my min date? so that tomorrow's min date is set to 2016-12-29 and so on. I don't want to keep setting the min date inside the html codes manually.
You can use date() function of php.
<input type="date" name="date" min="<?php echo date("Y-m-d", strtotime("+1days")); ?>">
Related
In my code, here is how i convert html date field values to unix timestamp before inserting to MySQL db which works fine:
//HTML Code:
<div class="form-group">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" required/>
</div>
<div class="form-group">
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" required/>
</div>
//PHP Code to convert to Time Stamp:
<?php $post->unix_stamp = strtotime($_POST['dateFrom']);
$post->unix_stamp_ex = strtotime($_POST['dateTo']);
$post->save(); ?>
How do i convert the unix timestamp back to html input date value for editing? What i've tried:
<input type="date" name="dateTo" value="<?php echo date("Y-m-d\TH:i:s",$post->unix_stamp_ex); ?>" required/>
Where $post->unix_stamp_ex is the unix timestamp value. (doesn't work for me, shows dd/mm/yyyy instead). My problem is how to get the underlying value, to display in the date input so users can edit it
The reason your input is showing a placeholder 'dd/mm/yyyy' is because it cannot read the format you gave it. It can't work with time, so you need to remove it from your code:
value="<?php echo date('Y-m-d',$post->unix_stamp_ex); ?>"
This will show the date in the input.
Also, mind that the displayed value will differ from the actual value format, as per my comment and RoussKS' answer.
According to Mozilla Developer docs for input type date
The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.
[edit]
Just noticed the comment response, was typing as El_Vanja commented
[/edit]
Brother, I am able to convert the database string back to readable date time using the following method.
Solutions
$databaseTime = '2020-03-22 09:45:48';
$date = strtotime($databaseTime);
echo "DatabaseTime: " . $databaseTime . "<br>";
echo "Date in epoch: ".$date."<br>";
echo "Readable date: ".date("Y-m-d H:i:s",$date)."<br>";
Results
https://prnt.sc/rkmleq
I have an HTML form that has a date input as below
<div class="col-sm" id="old">
<label for="startDateOld">Current Start Date</label>
<input type="date" class="form-control" id="startDateOlds" name="startDateOld" readonly value="<?php echo $startDate; ?>">
</div>
When the page loads, it is actually querying the startDate into a php variable called $startDate. I need this $startDate to be shown in my date input field. But somehow it is not showing anything.
In my MySQL database, this date is a timestamp. I format it the following php way to match the HTML date input
$startDate = date('Y/m/d', strtotime($startDate));
But somehow it is not loading the date value although $startDate contains a value in YYYY/MM/DD format which matches HTML date input format.
Does anyone know why I can't show the date value?
HTML date inputs need to be in YYYY-MM-DD form, not YYYY/MM/DD (see the manual). So change your:
$startDate = date('Y/m/d', strtotime($startDate));
to
$startDate = date('Y-m-d', strtotime($startDate));
and it should work fine.
Demo of YYYY-MM-DD and YYYY/MM/DD format in HTML:
<div class="col-sm" id="old">
<label for="startDateOld">Current Start Date</label>
<input type="date" class="form-control" id="startDateOlds" name="startDateOld" readonly value="2020-01-16"><br/>
<label for="startDateOld2">Current Start Date</label>
<input type="date" class="form-control" id="startDateOlds" name="startDateOld2" readonly value="2020/01/16">
</div>
I have a datatype "TIMESTAMP" in database. And I pass the value by using "datetime-local" attribute in html.
<input type="datetime-local" id="start_time" class="form-control" name="start_time"/>
this works fine and i can upload the date and time to database. But when i retrieve the data form database for editing, it doesn't work.
PHP Code
$query="SELECT * FROM hire WHERE hire_id='$id'";
$query_run=mysqli_query($con,$query);
$row=mysqli_fetch_array($query_run);
HTML Code
<input type="datetime-local" value="<?php echo $row['start_time']; ?>" />
The above php within the html shows nothing as a result. What is the fault here?
Because you have the input type set to 'datetime-local'. You have to have the date set to the right format in order for it to show up in the input.
Change your start_time value to be in this format.
$date = date("Y-m-d\TH:i:s", strtotime($row['start_time']));
Then in your input field echo out $date
<input type="datetime-local" value="<?php echo $date; ?>" />
first of all if you should get date from mysql then convert that string to date using strtotime function after that change the format of date and set it at value attribute of input form control haivng type="datetime-local".
$string_to_date=$d=strtotime($date_fromdatabase);
$new_date=Date('Y-m-d\TH:i',$string_to_date);
set as a value attribute of input element in laravel 6
<input type="datetime-local" class="form-control" value="{{ $new_date }}">
I'm creating a form with two date selectors. Currently, they look like:
<input type="date" class="form-control" name="contract_start" value="<?php echo $contract_start; ?>">
<input type="date" class="form-control" name="contract_end" value="<?php echo $contract_end; ?>">
Right now, on the form, the default values for the date selectors just say "mm/dd/yyyy". I'd like to know how I can alter this so that the contract_start input is the current date, and the contract_end input is 10 months from the current date by default. How can I do this?
Just set $contract_end to: date('m/d/Y', strtotime('+10 months', strtotime($contract_start)))
First strtotime call will convert raw date to timestamp, second will add ten months to your timestamp, and date function will format it back.
Strtotime reference
Date reference
You can use a single strtotime() function also in date function like this :
<?php
$contract_start = date('Y-m-d');
$contract_end = date('Y-m-d', strtotime($contract_start.' +10 months'));
?>
<input type="date" class="form-control" name="contract_start" value="<?=$contract_start;?>">
<input type="date" class="form-control" name="contract_end" value="<?=$contract_end;?>">
I have a date field and a time field for users to enter date and time, I then need to convert this to a time stamp. Here is my code
<input type='text' name='date' />
<input type='text' name='time' />
How can I put them both together and then generate a timestamp to place in database
any help appreciated
thanks
Use the strtotime() function:
$ts = strtotime($_POST['date'].' '.$_POST['time']);
try this...
$timestamp = strtotime('date');