Hi i am facing a problem while editing the plugin. the default time format of this plugin is 24 hours but i want to convert it into 12 hours with AM/PM as well.
Here is the code that i have
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group qc-input-container">
<select name="quickcab_form_departure_time_hour" id="quickcab_form_departure_time_hour" class="booking-input quickcab-select-input form-control" required>
<option disabled selected><?php echo esc_html__('Hour', 'quickcab'); ?></option>
<?php
for ( $i = 1; $i <= 12; $i++ ) {
?>
<option value="<?php echo sprintf('%02d', $i); ?>"><?php
echo sprintf('%02d', $i);
?></option>
<?php
}
?>
</select>
</div>
You can easily get the 12 hours time by using date() function. I'm giving an example for you.
// suppose your time is 19:24:15
$date = '19:24:15';
echo date('h:i:s a', strtotime($date));
The output will be
07:24:15 pm
You can get the output also by using DateTime
$date = new DateTime('19:24:15');
echo $date->format('h:i:s a') ;
h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
To keep it really simple you could just add another loop to 12 to get a full 24 hours, then a simple change to the sprinf() will get you the AM and PM, like this
sprintf('%02dAM', $i);
and in the second loop
sprintf('%02dPM', $i)
So your code
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group qc-input-container">
<select name="quickcab_form_departure_time_hour" id="quickcab_form_departure_time_hour" class="booking-input quickcab-select-input form-control" required>
<option disabled selected><?php echo esc_html__('Hour', 'quickcab'); ?></option>
<?php
// first 12 hours
for ( $i = 1; $i <= 12; $i++ ) {
?>
<option value="<?php echo sprintf('%02dAM', $i); ?>"><?php echo sprintf('%02dAM', $i);?></option>
<?php
}
// second 12 hours
for ( $i = 1; $i <= 12; $i++ ) {
?>
<option value="<?php echo sprintf('%02dPM', $i); ?>"><?php echo sprintf('%02dPM', $i);?></option>
<?php
}
?>
</select>
</div>
Related
I've got a form that loops (using while) all the months of the year in a drop-down. The value is the month in digits (m) and the text is the month in words (F). Everything displays correctly, yet when I post the form I'm getting a space after the 2 month digits. I've checked the form via an echo that the month digits are displaying without a trailing space, so it's definitely after posting. I've tried removing the trailing space with both "substr" and "rtrim" - neither remove the trailing space
Form code:
<div class="col-4">
<label for="phaseMonth" class="form-label ps-2 fw-bold">Draw-off Month</label>
<?php
$month = strtotime('2022-01-01');
$end = strtotime('2023-01-01');
$limonth = date('F', strtotime($row_lqe['drawoffDate']));
?>
<select name="phaseMonth" title="Select a Date" class="form-select border border-secondary" required>
<option value="">Select a Month</option>
<?php while ($month < $end) { ?>
<option value="<?php echo date('m', $month), PHP_EOL; ?>"
<?php if (!(strcmp(date('F', $month), $limonth))) {echo "selected=\"selected\"";} ?>>
<?php echo date('F', $month), PHP_EOL; ?>
</option>
<?php $month = strtotime("+1 month", $month); ?>
<?php } ?>
</select>
</div>
Post code:
$phaseMonth = $_POST['phaseMonth'];
if (empty($phaseMonth)) {
$newphaseMonth = "01";
} else {
$newphaseMonth = substr($phaseMonth, 0, -1);
}
$phaseYear = $_POST['phaseYear'];
if (empty($phaseYear)) {
$phaseYear = "2022";
}
$drawoffDate = $phaseYear . "-" . $newphaseMonth . "-01";
echo "$phaseMonth:$newphaseMonth:$phaseYear:$drawoffDate"; // to show trailing space
echo output:
11 :11 :2023:2023-11 -01
You seemed to miss the point about your excessive overuse of <?php and ?> tags; see this example and how much cleaner it is to view and work with:
<div class="col-4">
<label for="phaseMonth" class="form-label ps-2 fw-bold">Draw-off Month</label>
<?php
$month = strtotime('2022-01-01');
$end = strtotime('2023-01-01');
$limonth = date('F', strtotime($row_lqe['drawoffDate']));
print "<select name='phaseMonth' title='Select a Date'
class='form-select border border-secondary' required>
<option value=''>Select a Month</option>\n";
while ($month < $end) {
print "<option value='".date('m', $month)."'";
if (strcmp(date('F', $month), $limonth) === 0) {
echo "selected='selected'";
}
print ">".date('F', $month)."</option>\n";
$month = strtotime("+1 month", $month);
}
?>
</select>
</div>
Remove your PHP_EOL they're worthless here.
Use single and double quotes to double wrap quote marks rather than escaping quotes.
Use string concatenation . to streamline, as well as removing ?> <?php
This is a quick example only and can be further reduced but reduces readability.
Here is the code (HTML / PHP) :
<div class="col-sm-6">
<div class="form-group">
<select id="horseSize" name="horseSize" class="form-control" >
<option value="<?php echo htmlspecialchars($horseSize); ?>" selected><?php echo htmlspecialchars($horseSize); ?> m</option>
<?php
$i = 0.50;
while($i <= 2.00){
if ($i != $horseSize){
?>
<option value="<?php echo htmlspecialchars(number_format($i, 2)) ; ?>"><?php echo htmlspecialchars(number_format($i, 2)) ; ?> m</option>
<?php
$i = $i + 0.01;
}
}
?>
</select>
</div>
</div>
$horseSize comes from a SQL query and is working fine. Th problem is in displaying webpage.
When i am trying to display this webpage, my website goes down because of gateway giving no answer. This triggers only in one situation : if the variable value of $horseSize is inferior to 1. As if it was not possible to display something like 0,....
Maybe I did something wrong in this code, but I tried many times to fix, I couldn't reach it.
you are incrementing $i only under if condition, so value like 0.50 it will always never increase. Also floating point comparison will not work. please read: Floating point numbers
<div class="col-sm-6">
<div class="form-group">
<select id="horseSize" name="horseSize" class="form-control" >
<option value="<?php echo htmlspecialchars($horseSize); ?>" selected><?php echo htmlspecialchars($horseSize); ?> m</option>
<?php
$i = 0.50;
$precision = 0.00001;
while($i <= 2.00){
if(abs($i-$horseSize) > $precision) {
?>
<option value="<?php echo htmlspecialchars(number_format($i, 2)) ; ?>"><?php echo htmlspecialchars(number_format($i, 2)) ; ?> m</option>
<?php
$i = $i + 0.01;
} else {
$i = $i + 0.01;
}
}
?>
</select>
</div>
</div>
I'm having an stupid issue here; I'm trying to make in a form, a select with the time the user starts, and the time they finish, so logically, I want to display the time as:
00
01
02.. I mean, 2 digit
But although I put 00 for the initial value of $i, it displays only 1 digit anyway!
Is it any way I can set it to two digits?
Thanks!!
<div class="interval">
<input type="checkbox" id="interval_check"/>
<div id="desde">
De: <select class="hores" name="hores" disabled>
<?php for ($i = 00; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>:<select class="minuts" name="minuts" disabled>
<?php for ($i = 00; $i <= 45; $i+=15) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div> <div id="fins">
fins: <select class="hores" name="hores" disabled>
<?php for ($i = 00; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>:<select class="minuts" name="minuts" disabled>
<?php for ($i = 00; $i <= 45; $i+=15) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
just use sprintf to create the string for you.
<?php for ($i = 0; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>">
<?php echo sprintf("%02d", $i); ?>
</option>
<?php endfor; ?>
this will add a 0 if the string is less than 2 characters. I'm presuming that you don't care about the value.
I have the following HTML/PHP sign up form:
<div class="form_block" id="dob-check" style="display:none; float:left">
<p><label for="sign_up_dob_day">DOB</label> <select name="sign_up_dob_day" id="sign_up_dob_day" class="required"><option value="">-</option><?php for($day = 1; $day <= 31; $day++) : ?>
<option value="<?php echo $day ?>" <?php echo set_select('sign_up_dob_day', $day, ((is_array($dob) && count($dob) == 3 && $dob[2]==$day)? true : false)); ?>><?php echo $day ?></option>
<?php endfor; ?>
</select>
<select name="sign_up_dob_month" class="required">
<option value="">-</option>
<?php $months = getMonthsArray(); ?>
<?php for($month = 1; $month <= 12; $month++) : ?>
<option value="<?php echo $month ?>" <?php echo set_select('sign_up_dob_month', $month, ((is_array($dob) && count($dob) == 3 && $dob[1]==$month)? true : false)); ?>><?php echo $months[$month - 1] ?></option>
<?php endfor; ?>
</select>
<select name="sign_up_dob_year" class="required">
<option value="">-</option>
<?php for($year = 2005; $year >= 1930; $year--) : ?>
<option value="<?php echo $year ?>" <?php echo set_select('sign_up_dob_year', $year, ((is_array($dob) && count($dob) == 3 && $dob[0]==$year || (empty($dob[0]) && $year==1990)))? true : false); ?>><?php echo $year ?></option>
<?php endfor; ?>
</select>
<?php echo form_error('sign_up_dob_day'); ?>
<a class="hand" id="dob-why" onclick="showMessage()"> Why do we need this?</a></p>
</div>
Which displays as follows:
However, if a user fails to enter a DOB, the formatting of the select boxes gets completely messed up, as below:
I guess its because the width of the div is not enough when adding the 'field required' text.
I have tried putting an outer div but this did not work. I have also tried floating left, but this also did not work.
I am new to PHP so cannot see how to remove the second 'field required' message-this may possibly help?
Any other ideas eg prevent a new line being formed within the div?
Try using display: inline-block on inputs and error messages instead of display: block.
Should fix the problem.
I am trying to create two drop downs with HTML and PHP. The first is an auto submit that sends a month to a session variable:
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<?php $month = date('n'); // current month
for ($x = 0; $x < 12; $x++) { ?>
<option value="<?php echo date('m', mktime(0,0,0,$month + $x,1)); ?>"
<?php echo date('m', mktime(0,0,0,$month + $x,1)) == $_SESSION['month'] ? "selected='selected'":""; ?> >
<?php echo date('F Y', mktime(0,0,0,$month + $x,1)); ?>
<?php } ?>
</select>
</form>
This works great. But I then need a second drop down with the day name and number:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Select Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date)); { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?php } ?>
<?php } ?>
</option>
<?php } ?>
</select>
</form>
This uses a temp date with a session variable as '2014' which I set before. I need to remove the session year variable and get the first drop down to know which year the month selected is and then pass this to the temp date so that it populates the day name and number correctly for the next year (2015) if you choose January onwards. At the moment it only shows the day name and number from the current year (2014) in this case.
<?php
if(isset($_POST)) {
$date = explode('-', $_POST['month']);
$year = $date[0];
$month = $date[1];
echo "Year: ".$year." Month: ".$month;
}
?>
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('+%d months', $i));
$value = date('Y-m', $time);
$label = date('F Y', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
This gives me the year and month as separate values.
I had to check the print out of the first script, because it didn't make much sense...
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<option value="11"
>
November 2014 <option value="12"
>
December 2014 <option value="01"
>
January 2015 <option value="02"
>
February 2015 <option value="03"
>
March 2015 <option value="04"
>
April 2015 <option value="05"
>
May 2015 <option value="06"
>
June 2015 <option value="07"
>
July 2015 <option value="08"
>
August 2015 <option value="09"
>
September 2015 <option value="10"
>
October 2015 </select>
</form>
Might be I'm missing something, because it seemed to work, but it doesn't seem like you're closing the option tags. You are doing this:
<option value="11">November 2014
<option value="12">December 2014
Instead of this:
<option value="11">November 2014</option>
<option value="12">November 2014</option>
This doesn't seem to be the issue though, at least not in chrome, because when I checked the head data being sent, it was just fine:
month: 11
What happens when you send in the variable is that whatever page you send this form data with the method POST against will have to handle it further on. In PHP you do that with the global variable $_POST, in this case $_POST['month'].
if(!empty($_POST['month']))
{
echo $_POST['month'];
}
If you want to make it a session variable, the page you send the form data to will have to communicate with your browser to set this (through the reply HTTP header field). To do anything related to session in PHP you must first start a session using the session_start() before any HTML/data is sent against the browser, and the same goes for if you want to set a session variable using for example session_set_cookie_params. You also got setcookie, which also must be used before any data/HTML is sent to the browser and the variables can be accessed through $_COOKIE.
See if you can make some sense out of this, I've just used $_POST in this case, you can swap it with session or cookie and remember to send it before any data/html to the browser.
<?php
$selmonth = 0;
if(!empty($_POST['month']))
{
$selmonth = $_POST['month'];
}
$month = date('n'); // current month
echo '<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>';
for($i=0; $i<12; $i++)
{
$monthtime = mktime(0,0,0,$month + $i,1);
$monthnum = date('m', $monthtime);
echo '
<option value="'.$monthnum.'"'.
($monthnum == $selmonth ? ' selected="selected"' : '').
'>'.date('F Y', $monthtime).'</option>';
}
echo '
</select>
</form>';
?>
<select required="" class="btn btn-warning btn-raised" onchange="loadMonthYear(this)">
<?php
$fdt = date('Y-m-1');
$tdt = date('Y-m-1', strtotime("-12 months"));
while (date('Ymd', strtotime($fdt)) >= date('Ymd', strtotime($tdt))) {
echo '<option value="'.date('M, Y', strtotime($fdt)).'"><a data-tag="' . date('M, Y', strtotime($fdt)) . '" href="javascript: void(0);">' . date('M, Y', strtotime($fdt)) . '</option>';
$fdt = date('Y-m-1', strtotime("-1 months", strtotime($fdt)));
}
?>
</select>