Loop for Agenda with day hours and fill data from DB - php

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

Related

How do I create a automatic "next delivery time " text line in wordpress php?

So I have been coding a little custom line on my wordpress site using php, that should show the next delivery time automatically.
Right now it shows 2 automatically updating dates for 2 of the week days.
What I would want is them to cycle, so if it's less then 24 hours till lets say the fridays delivery you can't see that text. It would only show the other option.
This is the code that I currently have.
`
<p class="mb-4 fw-bold"><?php
_e('Järgmised tarneajad', 'esahver');
echo ": " ;
$now = strtotime("now");
$next_delivery = strtotime('next tuesday');
$limit_hours = 17;
$next_delivery_minus_hrs = strtotime('-' . $limit_hours . ' hours', $next_delivery);
$final_next_delivery = NULL;
if( $next_delivery_minus_hrs < $now ){ // past limit hours
$next2_delivery = strtotime('next tuesday', $next_delivery);
$final_next_delivery = wp_date( 'l, d.m.Y' , $next2_delivery);
} else {
$final_next_delivery = wp_date( 'l, d.m.Y' , $next_delivery );
}
$now1 = strtotime("now");
$next_delivery1 = strtotime('next friday');
$limit_hours1 = 17;
$next_delivery_minus_hrs1 = strtotime('-' . $limit_hours1 . ' hours', $next_delivery1);
$final_next_delivery1 = NULL;
if( $next_delivery_minus_hrs1 < $now ){ // past limit hours
$next2_delivery1 = strtotime('next friday', $next_delivery1);
$final_next_delivery1 = wp_date( 'l, d.m.Y' , $next2_delivery1);
} else {
$final_next_delivery1 = wp_date( 'l, d.m.Y' , $next_delivery1 );
}
echo $final_next_delivery;
echo "<input type='hidden' name='custom_delivery_date' value='".$final_next_delivery."'/>";
echo " & ";
echo $final_next_delivery1;
echo "<input type='hidden' name='custom_delivery_date' value='".$final_next_delivery1."'/>";
?>
</p>
`
How would I approach this problem?
Thanks
I have tried writing some bad if sentences that didn't work out at all.
You can refactor the code even more simply

How to create Php Timepicker in 12 hrs format

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>

How can I use a variable in a date diff function?

I am using an ACF Field to allow a client to content manage a countdown to their next event I am using JS flip clock on the desktop version but as it isn't responsive, I decided to use date diff to echo out just the number of days for mobile.
The site is currently live at theindustrialproject.co.uk
The code I currently have is this:
<?php
$date1 = date_create(date());
$date2 = date_create(the_field('mobile_date'));
$diff = date_diff($date1,$date2);
$difference = $diff;
if ($difference < 0) { $difference = 0; }
echo '<span class="countdown-mobile">'. floor($difference/60/60/24)."</span><br />";
if ($difference == 1) { echo "<p>Day</p>"; }
else { echo "<p>Days</p>"; }
?>
but it always returns 0. For reference, I pulled the code from here
Without knowing what the function the_field('mobile_date') will return ( either a date or timestamp? ) you might need to alter that particular line below but you should be able to use the DateTime object and format the difference like this
$format='Y-m-d';
$timezone=new DateTimeZone('Europe/London');
/* We need 2 datetime objects - one for now the other for the future date */
$now=new DateTime( date( $format, strtotime('now') ), $timezone );
$target=new DateTime( the_field('mobile_date'), $timezone );
/* Format the difference in days */
$days = $now->diff( $target )->days;
echo "<span class='countdown-mobile'>{$days}</span><br />" . ( $days == 1 ? '<p>Day</p>' : '<p>Days</p>' );

Timestamp wrong time

This is my problem
My default timezone is date_default_timezone_set('Europe/Madrid')
actual time 2012-12-06 17:00:38
start time 2012-12-10 16:30:00
unix timestamp actual time 1354809638
unix timestamp start time 1350052200
Start time is greater then actual time. But "unix timestamp actual time" is greater then "unix timestamp start time".
I wonder why?
RELEVANT CODE:
$to_unix_startTime = new DateTime($postField['startTime']);
$UnixStartTime = $to_unix_startTime->format('U');
$date = new DateTime();
$actualTime = $date->getTimestamp();
$start = new DateTime($postFields['startAuct']);
$start->format('Y-m-d H:i:s').PHP_EOL;
$start_tmstmp = date_timestamp_get($start);
$date = new DateTime();
$timestamp = $date->getTimestamp();
if ($timestamp > $start_tmstmp) {
echo $auctStatus = 1;
} elseif ($timestamp < $start) {
echo $auctStatus = 4;
}
$postField['startTime'] is in the wrong format. How do I know this? The timestamp you got from the start date, 1350052200, represents date 2012-10-12 (October 12th 2012) in stead of 2012-12-10 (December 10th 2012).
$postField['startTime'] is probably in the format 10/12/2012 (which will be parsed by DateTime as an American style date), when it should be formatted as 10-12-2012 (which will be parsed by DateTime as a European style date).
So, concluding, check the format of $postField['startTime'] and make sure it is a European style representation of a date.
[edit] Compare:
$european = new DateTime( '10-12-2012' );
$american = new DateTime( '10/12/2012' );
echo $european->format( 'Y-m-d' ) . ' (' . $european->getTimestamp() . ')' . PHP_EOL;
echo $american->format( 'Y-m-d' ) . ' (' . $american->getTimestamp() . ')' . PHP_EOL;
There is no need to format the datetime before getting the timestamp.
Check my code here: https://compilr.com/shadyyx/datetime/index.php
Should there be any problem with the link, here is the code without output:
<?php
date_default_timezone_set('Europe/Madrid');
$start = new DateTime('2012-12-10 16:30:00');
$actual = new DateTime('2012-12-06 17:00:38');
echo $start->format('d.m.Y H:i:s').PHP_EOL;
echo $actual->format('d.m.Y H:i:s').PHP_EOL;
echo PHP_EOL;
$start_tmstmp = $start->getTimestamp();
$act_tmstmp = $actual->getTimestamp();
echo $start_tmstmp.PHP_EOL;
echo $act_tmstmp.PHP_EOL;
echo PHP_EOL;
var_dump($start_tmstmp > $act_tmstmp);

Adjusting time zone in PHP with DateTime / DateTimeZone

There's a lot of info on doing time zone adjustments in PHP, but I haven't found an answer for specifically what I want to do due to all the noise.
Given a time in one timezone, I want to convert it to the time in another timezone.
This is essentially what I want to do, but I need to be able to do it using only the built-in PHP libs, not PEAR Date.
This is what I've been doing, but it seems to always give me the offset relative to GMT:
$los_angeles_time_zone = new DateTimeZone('America/Los_Angeles');
$hawaii_time_zone = new DateTimeZone('Pacific/Honolulu');
$date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone);
printf("LA Time: %s<br/>", $date_time_los_angeles->format(DATE_ATOM));
$time_offset = $hawaii_time_zone->getOffset($date_time_los_angeles);
printf("Offset: %s<br/>", $time_offset);
This is the output:
LA Time: 2009-09-18T05:00:00-07:00
Offset: -36000
I was expecting 3 hours (10800 seconds). but the '-7:00' thing tells me it's keeping everything relative to GMT, which maybe explains why it's giving me the "absolute" offset.
How do I just get the offset between the two timezones without all of this GMT hoohah?
Thanks.
UPDATE:
I occured to me that I could do this and get what I want:
$date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone);
printf("LA Time: %s<br/>", $date_time_los_angeles->format(DATE_ATOM));
$date_time_hawaii = new DateTime('2009-09-18 05:00:00', $hawaii_time_zone);
printf("Hawaii Time: %s<br/>", $date_time_hawaii->format(DATE_ATOM));
$time_offset = $los_angeles_time_zone->getOffset($date_time_los_angeles) - $hawaii_time_zone->getOffset($date_time_los_angeles);
printf("Offset: %s<br/>", $time_offset);
But it feels awkward to me. Anyone know a cleaner way to do it?
Here are a couple of functions using the DateTime classes. The first one will return the difference in seconds between two timezones. The second returns a "translation" of the time from one timezone to another.
function timezone_diff($tz_from, $tz_to, $time_str = 'now')
{
$dt = new DateTime($time_str, new DateTimeZone($tz_from));
$offset_from = $dt->getOffset();
$timestamp = $dt->getTimestamp();
$offset_to = $dt->setTimezone(new DateTimezone($tz_to))->setTimestamp($timestamp)->getOffset();
return $offset_to - $offset_from;
}
function time_translate($tz_from, $tz_to, $time_str = 'now', $format = 'Y-m-d H:i:s')
{
$dt = new DateTime($time_str, new DateTimezone($tz_from));
$timestamp = $dt->getTimestamp();
return $dt->setTimezone(new DateTimezone($tz_to))->setTimestamp($timestamp)->format($format);
}
Demo:
$los_angeles_time = '2009-09-18 05:00:00';
$los_angeles_tz = 'America/Los_Angeles';
$hawaii_tz = 'Pacific/Honolulu';
$los_angeles_hawaii_diff = timezone_diff($los_angeles_tz, $hawaii_tz, $los_angeles_time);
echo $los_angeles_hawaii_diff . '<br />';
$hawaii_time = time_translate($los_angeles_tz, $hawaii_tz, $los_angeles_time);
echo $hawaii_time . '<br />';
As GZipp commented, his code is really only for PHP >= 5.3.0. That is fine, but - here's a version that will work in PHP >= 5.2.0. (Incidentally, it also works in 5.3+, and with 2 less function calls)
<?php
function time_translate($tz_from, $tz_to, $time_str = 'now', $format = 'Y-m-d H:i:s')
{
$dt = new DateTime($time_str, new DateTimezone($tz_from));
$dt->setTimezone(new DateTimezone($tz_to));
return $dt->format($format);
}
$time_diffs = array('now', '-1 hour', '-1 day', '-1 week', '-1 month', '+1 hour', '+1 week', '+1 month');
foreach ($time_diffs as $diff)
{
echo "{$diff}:"
. "\n\t"
. "Current: " . date("Y-m-d H:i:s", strtotime($diff))
. "\n\t"
. "UTC: " . time_translate("US/Eastern", "UTC", $diff)
. "\n\n";
}
Simply minus the first time from the second.
That you'll be able to get the time difference in terms of seconds.

Categories