Query date and show in HTML date input - php

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>

Related

How to convert timestamp to html date input field value PHP

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

Input type="date" how to format?

I'm using a input type="date" but it always save to the database like this format.
Tue Sep 25 2018 00:00:00 GMT+0800 (Philippine Standard Time)
<form action="insertPTO.php" method="post" class="form-horizontal alert
alert-warning" name="reqTimeOff" >
<h3>Request Form</h3>
<div class="form-group">
<label for="from">From * :</label>
<input type="date" class="form-control" ng-model="fromDate" | date:"MM/dd/yyyy" autofocus required />
</div>
<div class="form-group">
<p class="text-danger" ng-show="reqTimeOff.fromDate.$invalid && reqTimeOff.fromDate.$dirty">Please select a Date!</p>
</div>
How do I set the format only like this format mm/dd/yyyy?
Thank you in advance.
You can't give format to this native HTML input.
Try to use DateTimePicker
I wouldn't - if you need to do any calculations in your database you will need it to be in a format that the database understands.
It sounds like what you actually want is for the date to display in a certain format in which case you can do this with PHP or a JavaScript library such as Moment.js.
In PHP there's the function date().
In your case you would do
$unformatted = "Tue Sep 25 2018 00:00:00 GMT+0800";
$formatted = date("m/d/Y", strtotime($unformatted));
Note: Most databases need you to format dates like this:
Y-m-d //for date
Y-m-d H:i:s //for datetime

manipulating date variables in php [duplicate]

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 6 years ago.
Hi I am trying to manipulate date input from the user.
I am accessing an api that requires a start date and end date, Currently I have the user entering the start date and the end date but I would ideally like the user to be able to input just the start date and the end date automatically assigned 7days later, I am currently using this code to assign the start and end date, Is it possible to just add 7days to the start date and assign that as the end date?
<div class="search-form">
<form action= "" method = "GET">
<div class="form-field">
<input type="type" name="start" placeholder= "yyyy-mm-dd" value="<?php $startDate; ?>"/>
</div>
</form>
<form action= "" method ="GET">
<div class="form-field">
<input type ="type" name= "end" placeholder="yyyy-mm-dd" value ="<?php $endDate;?>"/>
</div>
<button type="submit">Submit</button>
</form>
</div>
Please try this, e.g
$date = "Mar 03, 2011";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
Hope it will help you.
$date = "2016-08-24"
$startdate = date('Y-m-d', strtotime('+7 days',strtotime($date)));
You will get date after 7 days in YYYY-MM-DD format.

get the data to datetime-local from php

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 }}">

How To Placehold Date Inputs With Today & Later

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;?>">

Categories