My first block of code finds the nearest hour and stores it in a $nextHour variable.
I want to use this value in my second lot of code so it only shows future time in the dropdown. How do I update my second block of code to achieve this?
<?php
$date = new DateTime();
$nextHour = (intval($date->format('H'))+1) % 24;
echo $nextHour.':00'; // 5
?>
<?php
$start = "09:00";
$end = "20:00";
$tStart = strtotime($start);
$tEnd = strtotime($end);
$tNow = $tStart;
?>
<select name="callbacktime" id="callbacktime">
<?php
while($tNow <= $tEnd){
echo '<option value='.date("H:i",$tNow).'>'.date("H:i",$tNow).'</option>';
$tNow = strtotime('+1 hour',$tNow);
}
?>
</select>
$tStart = max([strtotime('09:00'), time()]);
I'd go with something like this as it saves having to do a check every loop as I previously mentioned in my comment.
Related
I would add the New text if one day is not passed but if it is passed i would delete the text New if one day is passed.
I tried this code :
<?php
$farkZaman = time() - strtotime("2017-09-26 19:00:00");
$farkGun = floor($farkZaman / (60 * 60 * 24));
if($farkGun < 1){
echo "Yeni";
}
?>
How can i do ?
I need your help.
Note : I have French but i don't have a good English.
Don't know if this is what you are looking for.
Here is set $dt as tomorrow with a time (about 24 hours from now GMT+1).
Then I compare if time is less than this time.
If it is I echo the text.
$dt = strtotime("2017-09-29 06:40");
If(time()<=$dt){
Echo "the text";
}
You can do something like this.
if(strtotime($date_variable) > strtotime("-1 day")) {
echo "New";
}
Try like this
<?php
$now = new DateTime();
$now->format('Y-m-d H:i:s');
$created_date = new DateTime(date('Y-m-d', strtotime("2017-09-27 13:00:00")));
$day = $created_date->diff($now)->days; // 0
if($day < 1){
echo "new texts";
}
?>
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 have a PHP script which records things based on the day. So it will have a weekly set of inputs you would enter.
I get the data correctly, but when i do $day ++; it will increment the day, going passed the end of the month without ticking the month.
example:
//12/29
//12/30
//12/31
//12/32
//12/33
Where it should look like
//12/29
//12/30
//12/31
//01/01
//01/02
My script is as follows:
$week = date ("Y-m-d", strtotime("last sunday"));
$day = $week;
$run = array(7); //this is actually defined in the data posted to the script, which is pretty much just getting the value of the array index for the query string.
foreach( $run as $key=>$value)
{
$num = $key + 1;
$items[] = "($num, $user, $value, 'run', '$day')";
echo "".$day;
$day ++;
}
Should I be manipulating the datetime differently for day incrementations?
You can use
$day = date("Y-m-d", strtotime($day . " +1 day"));
instead of
$day++;
See live demo in ideone
You refer to $day as a "datetime" but it is just a string - that is what date() returns. So when you do $day++ you are adding 1 to "2015-12-02". PHP will do everything it can to make "2015-12-02" into a number and then add 1 to it, which is not date math. Here is a simple example:
<?php
$name = "Fallenreaper1";
$name++;
echo $name
?>
This will output:
Fallenreaper2
This is how I would do it, using an appropriate data type (DateTime):
<?php
$day = new DateTime('last sunday');
$run = array(7);
foreach ($run as $key => $value) {
$num = $key + 1;
$dayStr = $day->format('Y-m-d');
$items[] = "($num, $user, $value, 'run', '$dayStr')";
echo $dayStr;
$day->modify('+1 day');
}
To increase time you should use strtotime("+1 day");
here is simple example of using it
<?php
$now_time = time();
for($i=1;$i<8;$i++) {
$now_time = strtotime("+1 day", $now_time);
echo date("Y-m-d", $now_time) . "<br>";
}
?>
I am creating a wordpress widget. This will allow user to upload the image url of eight different picture. Now I want to change the image everty three days and start from begining after eight one for infinite time.
I manage to get the start of the first image. let sat that is : 2014/07/28
I have applied this logic:
$date = $start_date;
$date = strtotime($date);
$date = strtotime("+3 day", $date);
$end_date = date('Y-m-d', $date);
$begin = new DateTime( $start_date );
$end = new DateTime('');
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
if($start_date <= $end_date && $end_date > date('Y-m-d'))
{
$image_number = 1;
}else{
$i = 1;
$j = 1;
foreach($daterange as $date){
if($i%3 == 0){
echo $image_number = $j;
$j++;
if($j > 8){
$j = 1;
}
}
$i++;
}
}
?>
" alt="">
Can anybody tell me what is wrong with the code.
Thank you everybody in advance for your valuable time.
Alright so it seems you have 8 images named 1.jpg to 8.jpg
Every three days you want to change the image to the next, and when you are at 8, you want to go to 1.
I don't know if you have some kind of start date on which the images were uploaded, and I don't know if it matters to you.
Let's start out with an example in which viewing the image is tied to the date (and resets on the first of january)
##maybe you would like to set the timezone
##date_default_timezone_set('America/Los_Angeles');
$dayOfTheYear = date('z');
$iterationCount = ceil($dayOfTheYear / 3);
$numberForIteration = $iterationCount % 8;
echo '<img alt="" src="'.$numberForIteration.'.jpg" />';
## prints for example: <img alt="" src="6.jpg" />
We can build on this and have an option of a different start date, meaning that not january 1st, but another date is the start date.
## PHP Version should be above June 2012
$startDate = new DateTime("2014-07-01");
$currentDate = new DateTime();
$daysSinceStart = $startDate->diff($currentDate)->days;
$iterationCount = ceil($daysSinceStart / 3);
$numberForIteration = $iterationCount % 8;
echo '<img alt="" src="'.$numberForIteration.'.jpg" />';
Hope it helps.
David
I've got to write a loop that should start and end between two times. I know there are many ways to skin this cat, but I'd like to see a real programmers approach to this function.
Essentially I have Wednesday, for instance, that opens at 6:00pm and closes at 10:30pm.
I'm looking to write a loop that will give me a table with all of the times in between those two in 15 minute intervals.
So, I basically want to build a one column table where each row is
6:00pm
6:15pm
7:15pm
etc...
My two variables to feed this function will be the open time and the close time.
Now don't accuse me of "write my code for me" posting. I'll happily give you my hacked solution on request, I'd just like to see how someone with real experience would create this function.
Thanks :)
$start = new DateTime("2011-08-18 18:00:00");
$end = new DateTime("2011-08-18 22:30:00");
$current = clone $start;
while ($current <= $end) {
echo $current->format("g:ia"), "\n";
$current->modify("+15 minutes");
}
Try it on Codepad: http://codepad.org/JwBDOQQE
PHP 5.3 introduced a class precisely for this purpose, DatePeriod.
$start = new DateTime("6:00pm");
$end = new DateTime("10:30pm");
$interval = new DateInterval('PT15M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $time) {
echo $time->format('g:ia'), PHP_EOL;
}
echo $end->format('g:ia'); // end time is not part of the period
$start = strtotime('2011-08-11 18:00:00');
for ($i = 0; $i < 20; $i++) {
echo date('g:ia', $start + ($i * (15 * 60))), '<br>';
}
I would go with the DateTime functions and increase the time by 15 minutes every loop-turn as long as the current time is lower then the end-time.
EDIT: as user576875 has posted
$start_date = '2019-07-30 08:00:00';
$end_date = '2019-09-31 08:00:00';
while (strtotime($start_date) <= strtotime($end_date)) {
echo "$start_date<br>";
$start_date = date ("Y-m-d H:i:s", strtotime("+1 hours", strtotime($start_date)));
}