I have used php timepicker in my project and it is 24 hrs format but in new requirement I need to implement this only 12 hrs format because I have to check time in AM PM format to compare with passed variable value. so what is the process to create in 12 hrs format
<?php
$time = strtotime('10:00');
$end_time = strtotime('20:00');
$end_time = date("h:i a",$end_time);
echo '<select>';
while($time <= $end_time){
$optionvalue = date("H:i",$time);
echo '<option value="'.$optionvalue
.'">'.$optionvalue.'</option>';
$time = strtotime('+15 minutes',$time);
}
echo '</select>';
?>
You need to use date('h:i a') to get meridiem am,pm
$end_time = strtotime('20:00');
$optionvalue = date("h:i a",$end_time);
echo $optionvalue;
Like Demo
/* start and end times converted to DateTime objects */
$start=new DateTime( date( DATE_ATOM, strtotime('10am') ) );
$end=new DateTime( date( DATE_ATOM, strtotime('8pm') ) );
$interval=new DateInterval('PT15M');
/* ensure the initial time is part of the output */
$start->sub( $interval );
$slots=array();
while( $start->add( $interval ) <= $end )$slots[]=$start->format('H:ia');
printf('<select name="times"><option>%s</select>', implode( '<option>', $slots ) );
document.querySelector('select[name="times"]').onchange=(e)=>{
alert( e.target.value )
}
<select name="times">
<option>10:00am
<option>10:15am
<option>10:30am
<option>10:45am
<option>11:00am
<option>11:15am
<option>11:30am
<option>11:45am
<option>12:00pm
<option>12:15pm
<option>12:30pm
<option>12:45pm
<option>13:00pm
<option>13:15pm
<option>13:30pm
<option>13:45pm
<option>14:00pm
<option>14:15pm
<option>14:30pm
<option>14:45pm
<option>15:00pm
<option>15:15pm
<option>15:30pm
<option>15:45pm
<option>16:00pm
<option>16:15pm
<option>16:30pm
<option>16:45pm
<option>17:00pm
<option>17:15pm
<option>17:30pm
<option>17:45pm
<option>18:00pm
<option>18:15pm
<option>18:30pm
<option>18:45pm
<option>19:00pm
<option>19:15pm
<option>19:30pm
<option>19:45pm
<option>20:00pm
</select>
Related
our site does festival reviews, and for this we want to be able to select a start date (datum) and an end date (eind_datum) via date time picker custom fields. Ultimately we want a function that determines whether the start and end date have the some month, so we end up with something like this
'1-4 Juli 2020' (instead of '1 Juli - 4 Juli 2020).
When the month is different we want it to be displayed as follows
'30 Juni - 4 Juli 2020'.
I have tried several things but none seem to work, the month needs to be in Dutch, I tried setting the locale but that didn't help, and the following code below throws an error at this line
'$first = $startdateTime->format('F');'
Anybody out here who might help ?
<?php
$start_date = get_field('datum');
$end_date = get_field('eind_datum');
$startdatetime = DateTime::createFromFormat('Y-m-d H:i:s', $start_date);
$enddatetime = DateTime::createFromFormat('Y-m-d H:i:s', $end_date);
$first = $startdateTime->format('F');
$second = $enddateTime->format('F');
if (get_field('eind_datum') and get_field('datum') and ($first == $second )) {
echo $start_date->format ('j'); echo "-"; echo $end_date->format ('j F Y') ;
}
elseif (get_field('eind_datum') and get_field('datum') and ($first != $second )) {
echo $start_date->format('j F'); echo "-"; echo $end_date->format ('j F Y') ;
}
else {
echo the_field('datum') ;
}
?>
The following should work:
$start_date = get_field('datum');
$end_date = get_field('eind_datum');
$startdatetime = new DateTimeImmutable($start_date);
$enddatetime = new DateTimeImmutable($end_date);
$first = $startdatetime->format('F');
$second = $enddatetime->format('F');
if ($start_date and $end_date and ($first == $second )) {
echo $startdatetime->format ('j'); echo "-"; echo $enddatetime->format ('j F Y') ;
}
elseif ($end_date and $start_date and ($first != $second )) {
echo $startdatetime->format('j F'); echo "-"; echo $enddatetime->format ('j F Y') ;
}
else {
echo $start_date;
}
The problem in you code is that the variables are sometime called incorrectly. For example: $start_date is not an object, and you cannot call the method format on it... or $startdateTime is not defined it is rather $startdatetime
Instead of creating 2 formats under certain conditions, you can also create the format '1 July - 4 July 2020' and then remove the month with preg_replace () if the months are the same.
$dateStr = '1 Juli - 4 Juli 2020';
$dateStr = preg_replace('~^(\d+) (\w+) - (\d+) (\2)~',"$1-$3 $2",$dateStr);
If the months are unequal, nothing is removed.
I got a little question I want to make a loop so I have a dropdown menu with the data. For example: A user has a beginning time of 12:00:00 and an end time of 14:00:00. The loop is supposed to pick out 30 min, so that you have a dropdown menu with 12:00:00, 12:30:00, 13:00:00, 13:30:00 and 14:00:00.
I will get the following data
$starttime = 12:00:00
$endtime = 14:00:00
$talktime = 00:30:00
now I want to make a dropdown menu from it that it loops.
here is my example how I thought it would work.
for ($endtime = $starttime + $talktime){}
But as the code works I want to check the database if the data already exist. I know how to do that part only the loop I don't know.
Already thank you for the help.
<?php
$starttime = new DateTime( '12:00:00' );
$endtime = new DateTime( '14:00:00' );
?>
<select>
<option></option>
<?php
for( $i = $starttime; $i <= $endtime; $i->modify( '+ 30 minutes' ) )
{
?>
<option> <?php echo $i->format( 'H:i' ); ?> </option>
<?php
}
?>
</select>
Each loop adds 30 minutes to the time.
You can try this
<?php
$starttime = new DateTime("12:00:00");
$endtime = new DateTime("14:00:00");
?>
<select name="" id="input" class="form-control" required="required">
<?php
while($starttime <= $endtime)
{
echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";
$starttime = date_add($starttime, date_interval_create_from_date_string('30 min'));
}
echo " </select>";
Try this
<?php
$starttime = "12:00:00";
$endtime = "14:30:00";
$starttime = strtotime($starttime);
$endtime = strtotime($endtime);
$tNow = $starttime;
while($tNow <= $endtime){
echo date("H:i:s",$tNow)."<br>";
$tNow = strtotime('+30 minutes',$tNow);
}
?>
I am stuck and I guess is a loop issue, can someone help me out here and tell me what is it that I am doing wrong? This is what I have:
$this->time = mktime(7,0,0,$this->date);
for($i=1;$i<56;$i++) {
echo '<tr><td>' . date('H:i', $this->time) . '</td>';
while($this->results = $this->sql->fetch(PDO::FETCH_ASSOC)){
if (date('H:i:s', $this->time) != $this->results['time']) {
$this->agenda = '<td>' . $this->results['name'] . '</td></tr>';
} else {
$this->agenda = '<td>This Hour is free</td></tr>';
}
echo $this->agenda;
}
$this->time += 900;
}
What I am trying to do and get is:
Display a range of time from 7am to 8:30pm using 24 hours format, also the hours should split every 15min, that's why I incremented the for loop on 900.
on the hours that match the db, show the name of the person, other wise show This Hour is free.
But I am having trouble with the table and loop/bucle order
Thanks and appreciate it
I hope this might be of use - it uses the DateTime, DateTimeZone and DateInterval classes but doesn't incorporate your code to check the db results - that should be fairly straightforward.
<?php
define('BR','<br />');
$df='H:i';
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('PT15M');
$ts=date( 'Y-m-d H:i:s', strtotime('6.45am') );
$tf=date( 'Y-m-d H:i:s', strtotime('8.30pm') );
$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );
while( $start->add( $interval ) <= $end ){
echo $start->format( $df ) . BR;
}
$timezone=$interval=$ts=$tf=$start=$end=null;
?>
I have two variables stored in my database containing the following data:
$date_published = 2012-05-04 00:00:00; //Straight from DB datetime field
$advert_duration = 15;
I want to show an advert for 15 days from the date it was published. To do so I need to work out the time difference.
I have read various places online about calculating time difference and have come up with the below code
In my attempt to work out the equation I can't seem to calculate the differences between $now - the date today, the $date_published and the $advert_duration. I can't get the correct result:
function days_left($date_published, $advert_duration){
$date = new DateTime($date_published);
$now = new DateTime();
$days_elapsed = $date->diff($now)->format("%d");
$days_left = $advert_duration - $days_elapsed;
return $days_left;
}
function getDaysLeft( $date, $duration )
{
// create $date and modify it by $duration
$date = new DateTime( $date );
$date->modify( sprintf( '+%d days', $duration ) );
// calculate the difference
$now = new DateTime();
$daysElapsed = (int) $now->diff( $date )->format( '%a' );
// set to negative value, if modified $date is before $now
if( $date < $now )
{
$daysElapsed = $daysElapsed * -1;
}
return $daysElapsed;
}
var_dump(
getDaysLeft( '2012-05-04 00:00:00', 15 ),
getDaysLeft( '2012-07-04 00:00:00', 15 )
);
If you're fetching your ad from the database, you can simply use a date function to calculate this :
WHERE DATE_SUB(CURDATE(),INTERVAL 15 DAY) >= date
Or you can do this in PHP (you'll get an UNIX timestamp) :
$date = strtotime('+15 days', strtotime($date_published));
I have an event calendar with start and end date like this:
16.08.2010 12:00:00 - 21.08.2010 20:00:00
16.08.2010 20:00:00 - 21.08.2010 23:00:00
18.08.2010 17:00:00 - 18.08.2010 19:00:00
Whenever an event goes on for more than one day, I need to loop through each day.
I found this thread, and I thought is could help me: How to find the dates between two specified date?
I can't use the solution for PHP 5.3, because my server run PHP 5.2.
The other solutions do not procude output.
This is what I try to do:
$events = $data['events'];
foreach($ev as $e) :
$startDate = date("Y-m-d",strtotime( $e->startTime ));
$endDate = date("Y-m-d",strtotime( $e->endTime ));
for($current = $startDate; $current <= $endDate; $current += 86400) {
echo '<div>'.$current.' - '.$endDate.' - '.$e->name.'</div>';
}
endforeach;
In theory, this should loop through all days for an event that extends over several days.
But that's not happening.
The logic is wrong somewhere.... please help :)
The problem is that you're trying to add numbers to a string. date('Y-m-d') produces a string like 2011-01-31. Adding numbers to it won't work [as expected]: '2011-01-31' + 86400 = ?.
Try something along these lines:
// setting to end of final day to avoid glitches in end times
$endDate = strtotime(date('Y-m-d 23:59:59', strtotime($e->endTime)));
$current = strtotime($e->startTime);
while ($current <= $endDate) {
printf('<div>%s - %s - %s</div>', date('Y-m-d', $current), date('Y-m-d', $endDate), $e->name);
$current = strtotime('+1 day', $current);
}
The date("Y-m-d") is wrong, you need the strtotime Result in your for loop. Try this, it should work:
$events = array(
array('16.08.2010 12:00:00', '21.08.2010 20:00:00', 'event1'),
array('16.08.2010 20:00:00', '21.08.2010 23:00:00', 'event2'),
array('18.08.2010 17:00:00', '18.08.2010 19:00:00', 'event3'),
);
$dayLength = 86400;
foreach($events as $e) :
$startDate = strtotime( $e[0] );
$endDate = strtotime( $e[1] );
if(($startDate+$dayLength)>=$endDate) continue;
for($current = $startDate; $current <= $endDate; $current += $dayLength) {
echo '<div>'.date('Y-m-d', $current).' - '.date('Y-m-d', $endDate).' - '.$e[2].'</div>';
}
endforeach;