Compare two time values using jQuery's validate - php

I have two selects, one that is a time from and the other is a time to. These values are split by fifteen minute integrals. I take those values and convert them using PHP's strtotime. I would like to compare these values to make sure that the time to is not lower than the time from or that the time from is not higher than the time to using jQuery's validate.
Thanks.
Here is my PHP:
$start = strtotime('12:00 AM');
$end = strtotime('12:00 PM');
echo '<select name="TimeFrom" id="TimeFrom" class="timeSelectFrom">';
for ($i = $start; $i <= $end; $i += 900)
{
echo '<option>' . date('g:i A', $i) . '</option>';
}
echo '<option>Closed</option>';
$start = strtotime('12:00 AM');
$end = strtotime('12:00 PM');
echo '<select name="TimeTo" id="TimeTo" class="timeSelectTo">';
for ($i = $start; $i <= $end; $i += 900)
{
echo '<option>' . date('g:i A', $i) . '</option>';
}
echo '<option>Closed</option>';
echo '</select>';

You could easily translate the values from the form elements into a basic time-stamp using a simplistic formula like (seconds+60*minutes) and then compare them as required.

Related

How to loop through time in select box PHP

I have a select drop down where I want to list times in the format of HH:MM with 1 minute intervals. so the list will start at 00:00 and finish at 23:59
I understand how to create a loop in a select drop down that will output 0-10
<select><?php for($i=0; $i<10; $i++){echo "<option>" . $i . "</option>";} ?>
</select>
and I understand how to output the time as HH:MM
<option><?php echo date('h:i', $supportrequest->startTime); ?</option>
But I can't work out how to do a combination of the two as I'm not sure what the parameters of the for loop should be
Using DatePeriod it'd be like that:
<?php
$begin = (new DateTime())->setTime(0,0,0); // create start point
$end = (new DateTime())->setTime(23,59,59); // create end point
$interval = new DateInterval('PT1M'); // set the interval to 1 minute
$daterange = new DatePeriod($begin, $interval ,$end); // create the DatePeriod
echo "<select>";
foreach($daterange as $date){ // loop through that period
echo "<option value='".$date->format("H:i") . "'>".$date->format("H:i")."</option>\n";
}
echo "</select>";
Using these classes makes it now easy to modify if you f.e. only want to have every 30 minutes, or need a different output format.
Do you really want to have a drop-down with one thousand, four hundred and forty option values (24 * 60 = 1440)? I think it would be better to have two <select> elements. You could style them to sit next to each other with a : in the middle if you wanted to keep the 'H:m' look.
<select id="hours">
<?php
for ($h = 0; $h < 24; $h++) printf("<option value=\"$h\"" . (!$h ? " selected" : "") . ">%02d</option>", $h);
?>
</select>
<select id="minutes">
<?php
for ($m = 0; $m < 60; $m++) printf("<option value=\"$m\"" . (!$m ? " selected" : "") . ">%02d</option>", $m);
?>
</select>
Converted Mukyuu's comment into an answer:
<select>
<?php
for($h=0; $h<24; $h++){
for($i=0; $i<60; $i++){
$time = date('h:i',strtotime($h.':'.$i));
echo "<option>".$time."</option>";
}
}
?>
</select>

Working dates between two given dates in Php

Please, i need assistance in this code.I have checked others in Stakeoverflow, but it is not combatible, hence this question. I want to generate all working /weekdays between two dates.I have found a code, but it is generating all days, including weekend. How do i eliminate the weekend from the list or ensure the list generated is ONLY for weekdays?
<?php
$start_Date = date('Y-m-d');
$end_Date = date('Y-m-d', strtotime('30 weekdays'));
//echo $start_Date."<br/>";
//echo $end_Date."<br/>";
// Specify the start date. This date can be any English textual format
$date_from = $start_Date;
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = $end_Date;
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
$c = 0;
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
I expect 30days to be generated but with this code, I am getting 42days . Weekend has been added,instead of weekdays ONLY .
Just add this to your loop:
$w = date('w',$i);// day of week - Sunday == 0, Saturday == 6
if($w == 0 || $w == 6){
continue;
}
DEMO
Your code is almost working only have to add a if checking in your code
your code
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
please replace with that one
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$day = date("w", $i);
if($day != 0 && $day!= 6){ // will continue if not Sunday or Saturday
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
}
You also can take help from php.net
Thanks
You may need to get the day of the week, like date("D"), then use it in your for loop to check..something like this?:
$Weekends = array("Sat","Sun");
for....
$DayOfWeek = date("D",$i);
if(!in_array($DayOfWeek, $Weekend)){
// increment...
}

Use static keyword in a for loop?

I want to use the static keyword in my for loop.
Is it possible? if yes then how?
Here is my code:
$current_time = date('h:i A');
<select>
for($i = 0; $i <= 5; $i++) {
if ($i=0) {
echo "<option>" . date("h:i A", $current_time) . "</option>";
}else{
static $tNow = strtotime("+15 minutes",strtotime($current_time));
echo "<option>" . date("h:i A", $tNow) . "</option>";
}
}
<select>
When I'm using the static keyword, I'm getting a php error.
I want to display a <select> element with each option being 15 minutes steps, like 12:00, 12:15, 12:30.
I don't know why you want to use static keyword in your code, but as suggested you should read here: http://php.net/manual/en/language.oop5.static.php, how and when to use static keyword.
If your goal is to display dropdown and getting option value different 15mint time
like this 12:00, 12:15, 12:30..... this is my solution:
$current_time[0] = date('h:i A');
echo"<select>";
echo "<option>" . $current_time[0] . "</option>";
$step=0;
for($i = 1; $i <=5; $i++) {
$step=$step+15;
$current_time[$i] = strtotime("+".$step." minutes", strtotime($current_time[0]));
echo "<option>" . date("h:i A", $current_time[$i]) . "</option>";
}
echo"</select>";
var_dump($current_time);
Working code here: http://ideone.com/zM0eue
Explanation: I used an array to contain all strtotime output in for loop. Each time I used the same $current_time[0] to calculate new $current_time[$i] with a $step variable incremented by +15 each time.

Performance tune PHP Time Query

I was wondering if I could do some performance tuning for the below PHP Script. What I am trying to do - is get every half hour from 00:00am to 23:30pm into a select list. Here is the code
<?php
$starttime = '00:00';
$time = new DateTime($starttime);
$interval = new DateInterval('PT30M');
$temptime = $time->format('H:i');
do{
echo '<option value="">'.date("H:i a", strtotime($temptime)).'</option>';
$time->add($interval);
$temptime = $time->format('H:i');
}while ($temptime !== $starttime);
?>
I believe there is a way we can do it in an easier manner - but I cannot think of it. Can someone help?
Other than hardcoding the values, you can lose all the DateTime and TimeInterval objects and use a simple loop:
for ($h=0;$h<24;$h++) {
echo '<option>' + sprintf ( "%02d", $h ) + ':00</option>';
echo '<option>' + sprintf ( "%02d", $h ) + ':30</option>';
}
If performance is what you're after, hardcoding the time strings is probably the best thing you can do.
Since they won't change, you don't really need them dynamic, would you?
foreach (range(0, 60*24, 30) as $minutes) {
echo '<option>' . date('H:i a', strtotime('00:00 +' . $minutes . ' minutes')) . '</option>';
}
Till day has 24h, you can use a good ol' for:
<select>
<?php for($i = 0; $i < 48; $i++): ?>
<option><?php echo $i%2 == 0 ? ($i / 2) . ':00' : intval($i / 2) .':30'; ?></option>
<?php endfor;?>
</select>

PHP Select every other Wednesday

I need help Select every other Wednesday starting on 5/2/12. This code below selects every other Wednesday starting on the week it currently is. But i need to set the beginning week. I am familiar with PHP, but not familiar with php dates. So please be as specific as possible.
I found this:
$number_of_dates = 10;
for ($i = 0; $i < $number_of_dates; $i++) {
echo date('m-d-Y', strtotime('Wednesday +' . ($i * 2) . ' weeks')). "<br>".PHP_EOL;
}
Use mktime to create your starting date and pass that as the second argument to strtotime so that counting starts from there:
$startDate = mktime(0, 0, 0, 5, 2, 2012); // May 2, 2012
for ($i = 0; $i < $number_of_dates; $i++) {
$date = strtotime('Wednesday +' . ($i * 2) . ' weeks', $startDate);
echo date('m-d-Y', $date). "<br>".PHP_EOL;
}
See it in action.
Give it a date in the string, instead of "Wednesday" (that chooses the next Wednesday), write:
strtotime('20120502 +' . ($i * 2) . ' weeks'))
To choose that date. (Format is yyyymmdd).
If you have PHP 5.2.0 or newer, you can do it easily this way:
$date = new DateTime('2006-05-02');
for ($i=0; $i<10; $i++) {
echo $date->format('m-d-Y').'<br/>'.PHP_EOL;
$date->modify('+1 week');
}
You could also use the DatePeriod and DateInterval classes to make life easier.
Standard disclaimer: both of the classes above require PHP >= 5.3.0.
$number_of_dates = 10;
$start_date = new DateTime("5/2/12");
$interval = DateInterval::createFromDateString("second wednesday");
$period = new DatePeriod($start_date, $interval, $number_of_dates - 1);
foreach ($period as $date) {
echo $date->format("m-d-Y") . "<br>" . PHP_EOL;
}

Categories