I have really been trying hard to achieve the following:
<option value="2011-11"><? echo $month[11]?> '11</option>
<option value="2011-12"><? echo $month[12]?> '11</option>
<option value="2012-1"><? echo $month[1]?> '12</option>
<option value="2012-2"><? echo $month[2]?> '12</option>
Where the following are dynamic according to today's date:
<option value="YYYY-m"><? echo $month[12]?> 'yy</option>
and repeating the process for the following 12 months ahead of today.
I made some progress with my rudimentary knowledge of PHP, I have admitted I need help from those more knowledgeable than myself!
It does look simple and I often look at stackoverflow to solve things though on this occasion I have spent far too much time and made too little progress.
Can anyone give me a hand to this seemingly simple challenge!?
Thank you!!!
Commenting for anybody who finds question.
The accepted answer is wrong. See here:
http://www.php.net/manual/en/function.strtotime.php#107331
use this instead:
<?php
for($i=0;$i<=12;$i++)
{
$time = strtotime("first day of +". $i." months");
print '<option value="'. date("Y-m", $time) .'">'.date("Y-m",$time).'</option>';
}
?>
The fix being "first day of"
This snippet is proof of concept achieving exactly what you want.
for($i=0;$i<=12;$i++)
{
$time = strtotime("today + ". $i." months");
print '<option value="'. date("Y-m", $time) .'">'.date("Y-m",$time).'</option>';
}
If you want to print the name of the month in your specific language, you should take a look at setlocale and just use the F parameter in date
Here you go:
$time = time();
foreach (range(0, 12) as $i)
{
echo '<option value="' . date('Y-m', $time) . '">' . $month[date('m', $time)] . date('y', $time) . '</option>' . "\n";
if ($i >= 1)
{
$time = strtotime('next month', $time);
}
}
Related
Im a bit stumped as i have some code to generate a simple dropdown of date for 21 days in advance.
It works fine as such however the output is not showing the right date instead its showing 1st Jan 1970
$timestamp = strtotime('today');
$output = [];
for ($day = 0; $day < 21; $day++) {
$output[] = date('d M y', strtotime(sprintf('+%d days', $day), $timestamp));
}
echo "<select name='days'>";
foreach ($output as $day)
{
echo "<option value='".$day."'>".date('l jS \of F Y',$day)."</option>";
}
echo "</select>";
if i echo just $day as the outpout value it displays in the DD/MM/YYYY format but i want it to display Day Name Date of Month Year
so whats not correct with using
date('l jS \of F Y',$day)
Im sure its something dumb but ive checked over all the various posts around on date but i cant for the life of me figure this out.
The function date() takes as its second argument a time in seconds.
If you look at the PHP:date documentation, you'll find that the second argument expects an integer, not a string.
Change your code to the following:
<?php
$output = [];
for ($day = 0; $day < 21; $day++) {
$output[] = strtotime(sprintf('+%d days', $day));
}
echo "<select name='days'>";
foreach ($output as $day)
{
echo "<option value='".$day."'>".date('l jS \of F Y', $day)."</option>";
}
echo "</select>";
Rather than filling your output array with strings, you'll see here that we instead fill it with 21 timestamps.
Once you begin looping through your output array, passing in the timestamp to the second argument of the date function will yield the output you're expecting.
I'm keeping the value of each option set to that timestamp, as it makes everything easier to work with if you need to process any POST data. But you're free to adjust the formatting there as needed.
Here's a working example that you can play around with.
Edit:
Just for funsies, here's how you can do it in a more object-oriented style. I feel it's a little more succinct this way, but it's up to you if you want to take this approach or not.
<?php
$today = new DateTime();
echo "<select name='days'>";
for ($day = 0; $day < 21; $day++)
{
echo "<option value='".$today->getTimestamp()."'>".$today->format('l jS \of F Y')."</option>";
$today->add(new DateInterval("P1D"));
}
echo "</select>";
Okay so I have a function that compares the hours from 00:00:00 - 24:00:00 to tell the closing time. Now the issue I have is, lets say the store opens at 8AM and closes at midnight, well 00:00:00is going to be less than 23:00:00. How would I go about making sure that it recognizes that midnight is greater?
Here is what I am doing:
if((strtotime($openTime) < time()) && (strtotime($closeTime) > time()))
{
$response['data_retrieved']['store'][$i]['store_settings']['open'] = 1;// open
}
else
{
$response['data_retrieved']['store'][$i]['store_settings']['open'] = 0; //close
}
Suggestions?
Since there is no 24:00:00, you'll need the full date string Y-m-d H:i:s to do an accurate comparison.
Even just adding in the date won't be enough, as it will break when looking at the last day of the month ('31 08:00:00' < '1 00:00:00').
PHP has a very good DateTime support. Its better to use that instead of reinventing things. Also like #mopo922 suggested, you'll need the full Y-m-d H:i:s
The answer #mopo922 provided above is the most foolproof and most correct answer.
Another method would be to "add" 24 hours to the $closeTime if $startTime > $closeTime. It's quite dodgy but still solves the problem.
<?php
date_default_timezone_set('Australia/Sydney');
$openTime = strtotime('08:00');
$currentTime = strtotime('23:00');
$closeTime = strtotime('10:00');
echo 'open: ' .$openTime . '<br>' .
'time(): ' . $currentTime . '<br>' .
'close: ' . $closeTime . '<br>';
// 1 day = 86400 seconds
if ($closeTime < $openTime) {
$closeTime += 86400;
}
if($openTime < $currentTime && $closeTime > $currentTime)
{
echo 'open';
}
else
{
echo 'closed';
}
?>
Hi I am creating a website for a restaurant delivery service. Basically when the customer is checking out, he/she can choose when they want the food to be delivered. I want the select box to contain time intervals of 15 mins ranging from the current time until the close time. The restaurant is open for deliveries between 11:00 to 23:00. The first option I want is to be "As soon as possible", and then the next option is an hour later (rounded to nearest 15mins), and then 15 mins each time. So basically something like this:
(Suppose current time is 13:55)
As soon as possible
15:00
15:15
15:30
15:45
...and so on until close time (23:00)
If the restaurant is closed (after 23:00) then I just want the select box to have an option that says "CLOSED".
Here is what I have tried so far but it is not working:
<select>
<?php
$timenow = date("H:i");
if($timenow >"23:00" || $timenow < "11:00"){
echo '<option value="Closed">CLOSED</option>';
echo "</select>";
}
else{
$deliverytime = date("H:i", strtotime('+15 minutes', $timenow));
echo '<option value="asap">As soon as possible</option>';
while($deliverytime < "23:00" && $deliverytime > "11:00"){
echo '<option value="'. $deliverytime.'">' . $deliverytime . '</option>';
$deliverytime = date("H:i", strtotime('+15 minutes', $deliverytime));
}
echo "</select>";
}
?>
strtotime('+15 minutes', $timenow) is not correct. The second argument should be a timestamp, not a string. You want something like strtotime('+15 minutes', time()) or just leave off the second argument (current time is the default).
A better approach is to always work with the timestamps until you output. That makes rounding and comparisons much easier.
<select>
<?php
$timenow = time();
$opentime = strtotime('11:00');
$closetime = strtotime('23:00');
if($timenow > $closetime || $timenow <= $opentime){
echo '<option value="Closed">CLOSED</option>';
echo "</select>";
}
else{
// you said you wanted the time to start in 1 hour, but had +15 minutes...
$deliverytime = strtotime('+1 hour', $timenow);
// round to next 15 minutes (15 * 60 seconds)
$deliverytime = ceil($deliverytime / (15*60)) * (15*60);
echo '<option value="asap">As soon as possible</option>';
while($deliverytime <= $closetime && $deliverytime >= $opentime) {
echo '<option value="'. date('H:i', $deliverytime) .'">' . date('H:i', $deliverytime) . '</option>'."\n";
$deliverytime = strtotime('+15 minutes', $deliverytime);
}
echo "</select>";
}
I can't provide a complete solution but can point you in the right direction.
This code will handle the 15 minute interval parts for you:
$start = new DateTime();
$end = new DateTime('11PM');
$interval = new DateInterval('PT15M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("l Y-m-d") . PHP_EOL;
}
The accepted answer in this Stack Overflow question shows how to get to the nearest 15 minute interval: PHP DateTime round up to nearest 10 minutes
Combine the two and you should have a complete solution to your problem.
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>
This should be a simple fix;
I currently have a calendar for selecting a range of date:
http://protekco.com/index.php/en/reservations/1718-carvelle-drive-westpalm-beach.html
When the dates are selected in the calendar, it populates 2 input fields for Check in and Check out dates. The problem is, the calendar initially was set 1 day late, so Thursday Sept 22 actually showed as a Thursday Sept 21. I was able to just echo a +1 on the day count, but the input boxes still show the erroneous date. Essentially, I'm trying to add the same +1, but because the date is returned as yyyy/mm/dd, +1 doesn't do much.
Here is the code:
$listsFrom = $this->lists['from'];
$selectedFrom = $listsFrom ? strtotime($listsFrom) : 0;
$listsTo = $this->lists['to'];
$selectedTo = $listsTo ? strtotime($listsTo) : $selectedFrom;
if ($selectedTo) {
ADocument::addDomreadyEvent('Calendars.checkOut = ' . $selectedTo . ';');
}
if ($selectedFrom) {
ADocument::addDomreadyEvent('Calendars.checkIn = ' . $selectedFrom . ';');
}
$listOperation = $this->lists['operation'];
ADocument::addDomreadyEvent('Calendars.operation = ' . $listOperation . ';');
Any ideas?
Thank you!
Edit:
<td class="<?php echo implode(' ', $class); ?>" <?php if ($canReserveBox) { ?> id="day<?php echo $firstDay->Uts+84600000; ?>" onclick="Calendars.setCheckDate('day<?php echo $firstDay->Uts+84600000; ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_MYSQL_DATE, 0); ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NORMAL); ?>')"<?php } ?> >
<span class="date" >
<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NICE_SHORT)+1;
?>
</span>
This is what actually calls the changes. The + 84600000 is my addition, it doesn't seem to do much tho.
If the date is in yyyy/mm/dd format, first use strtotime() to convert it to a timestamp, and then you can use it again to add one day.
$date = '2011/06/01';
$ts = strtotime('+24 hours', strtotime($date));
$date = date('Y/m/d', $ts);
That is just one possible solution.