I have seen all similar answers and they all tell us the same thing. But You do need a loop inside echo like a date selector with time?
Is there a nice way with just 1 For Loop to do all date & time?
Edit:
I am so sorry, my code indeed is terrible and functions just didn't cross my mind. I'll keep that in mind, all perfect answers thankyou!
That's why functions have been created...
function displayOptions($start, $end)
{
// insert here some tests on start and end
for($x = $start ; $x <= $end ; $x++) {
echo "<option value=\"$x\">$x</option>";
}
}
Create function and then use it:
function options($start, $end, $name, $label = '') {
echo "{$label}<select name='$name'><option></option>";
for($x = $start; $x <= $end; $x++) { echo "<option value='$x'>$x</option>"; }
echo "</select>";
}
options(1, 31, 'day', 'Date:');
options(1, 12, 'month', '/');
options(2012, 2022, 'year', '/');
options(0, 23, 'hour', 'Time:');
options(0, 59, 'minute', ':');
Create a function:
// $min contain start value
// $max contain end value
function create_list($min, $max) {
echo "<select name=''><option></option>";
for($x=$min;$x<=$max;$x++) { echo "<option value='$x'>$x</option>"; }
echo '</select>';
}
// call function as many time as you want to generate selectbox
create_list(1,31);
create_list(1,12);
create_list(2012,2022);
create_list(1,24);
create_list(1,59);
Related
I want to make a loop that works in 2 ways , if period is anynumber but 13 and it's multiplications , it will call the last income I had , if not it should make the equations you see below , any idea how to ?
for ($i=1; $i <= $period ; $i++) {
if ($i+=13) {
echo round($income=$income*(1+($rowage/100))). "<br>";
} else
{
echo $income. "<br>";
}
}
It is called mutiple of 13, and you can use modulos:
for ($i=1; $i <= $period ; $i++) {
if ($i%13==0) {
echo round($income=$income*(1+($rowage/100))). "<br>";
} else
{
echo $income. "<br>";
}
}
$i%13==0 means if division of i by 13 gives 0, which should be true if i is multiple of 13.
i want to convert month name to month number. By using this code, it is only show a result for december, the other month didnt work. But it is work if i change the year. For example, i choose November and 2015, the result is December and 2015. and if i choose November and 2014, the result is December and 2014.
The value in the database is 2015-09-28. i think there is a mistake on how i convert month name to month number. Can someone help me to fix my code.
This is my code :
VIEW
<?php echo form_open("announcement/announcement_result");?>
<?php echo form_dropdown('m', $m, set_value('m'), 'id="m"'); ?>
<?php echo form_dropdown('q', $q, set_value('q'), 'id="q"'); ?>
<?php echo form_submit('search', 'SEARCH', 'class="button expand"'); ?>
<?php echo form_close(); ?>
CONTROLLER
function announcement_list()
{
$data['q'] = array(
'' => ' Select Year',);
for ($i = 0; $i < 10; $i++)
{
$date = date('Y') - $i;
$data['q'][$date] = $date;
}
$m = '';
$data['m'] = $m;
$data['m'] = array(
'' => 'Select Month',
);
for ($m = 1; $m <= 12; $m++) {
$month = date("F", mktime(0, 0, 0, $m));
$data['m'][$month] = $month;
}
if ($m='December')
{
$m='12';
}
else if($m='November')
{
$m='11';
}
else if ($m='October')
{
$m='10';
}
else if ($m='September')
{
$m='9';
}
else if ($m='August')
{
$m='8';
}
else if ($m='July')
{
$m='7';
}
else if ($m='June')
{
$m='6';
}
else if ($m='May')
{
$m='5';
}
else if ($m='April')
{
$m='4';
}
else if ($m='March')
{
$m='3';
}
else if ($m='February')
{
$m='2';
}
else if ($m='January')
{
$m='1';
}
$data['results'] = $this->news_model->get_announcement_list($config['per_page'], $page);
}
MODEL
function get_results($m, $q, $limit=6, $offset=0)
{
$sql = "SELECT *
FROM ArkibBerita
WHERE code='PENGUMUMAN' AND Enable = 'Y' AND Lang ='EN' AND YEAR(BeritaDate)='{$q}' AND MONTH(BeritaDate)='{$m}'
ORDER BY position ASC
OFFSET {$offset} ROWS
FETCH NEXT {$limit} ROWS ONlY";
$query = $this->db->query($sql);
return $query->result();
}
What about:
echo date('m', strtotime('january'));
Output:
01
If you don't want the leading zero use n or see the manual for other usages; http://php.net/manual/en/function.date.php.
In your code you aren't comparing the date, you are setting it.
if ($m='December')
Should be
if ($m=='December')
One equals sets. Two equals compares. Three equals compares and requires the same variable type. http://php.net/manual/en/language.operators.comparison.php
So on every iteration your $m is going to be 12 because the $m always sets to the string and that is the first condition it hits. If you inverted your order it would be set to 1.
You also should look into using prepared statements for your SQL queries. http://php.net/manual/en/security.database.sql-injection.php
I'm trying to echo a random date in a PHP loop, the code works for "if the post is 3 months old", then I want to get today's date, and minus the "random" number, eg will echo a date based on this number.
Everything is working, but not the $number. Can someone please tell me where I'm going wrong?
<?php
$post_age = date('U') - get_the_time('U');
if($post_age > 7884000 ) {
?>
<?php $number = 'UniqueRandomNumbersWithinRange(0,25,5)'; ?>
<?php echo date('jS F', strtotime("now -'.$number.' days") ); ?>
<?php } else {?>
<?php } ?>
with function:
function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}
Thanks :)
Two issues that I see there which are likely causing the issue, there are two occasions where you are turning things into strings but don't want to:
for
this isn't running the function because it's between single quotes ' ' so you want to remove them
<?php $number = UniqueRandomNumbersWithinRange(0,25,5); ?>
and for
You are mixing different types of quotes and so $number isn't really being called. You likely want to adjust it to something like:
<?php echo date('jS F', strtotime("now -".$number." days") ); ?>
You are confusing strings and functions several times in your code, as James' answer says.
Also, your UniqueRandomNumbersWithinRange() function returns an array. This cannot be written to a string.
<?php
$post_age = date('U') - get_the_time('U');
if($post_age > 7884000) {
$number = fetchRandoms(0, 25, 5);
echo date('jS F', strtotime("now -" . $number . " days") );
} else {
// la dee da
}
function fetchRandoms($min, $max, $amt) {
$result = '';
for ($i = 0; $i < $amt; $i++) {
$result .= mt_rand($min, $max);
}
return $result;
}
$array_of_time = array ();
$start_time = strtotime ("$app_date 07:00");
$end_time = strtotime ("$app_date 22:00");
$mins = 04 * 60;
while ($start_time <= $end_time)
{
$array_of_time[] = date ('h:i a', $start_time);
$start_time += $mins;
}
echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";
foreach($array_of_time as $key=>$value)
{
for($i = 0; $i < count($app_data); $i++)
{
$book_time=$app_data[$i]["appointment_time"];
if($value==$book_time)
{
echo "<a class='time_slot_a_book'>$value</a> ";
} else {
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
}
echo "</div>";
Here foreach loop can run as many time as it can as well as for loop also, but i want to show the links that are not matched with the foreach value and for loop value.
The foreach loop values like 7:20 am not from database but the for loop value like 7:20 am is from database so if 7:20 am==7:20 am then the if statement it run it is working fine, but the issue is that it is running 2 time if i get 2 value in for loop. It should run my div only once.
Use a array to store the item that has shown.
<?php
foreach($array_of_time as $key=>$value)
{
$tag_list = array();
for($i = 0; $i < count($app_data); $i++)
{
$book_time=$app_data[$i]["appointment_time"];
// shown, skip
if (isset($tag_list[$book_time]))
{
continue;
}
// mark as show
$tag_list[$book_time] = 1;
if ($value == $book_time)
{
echo "<a class='time_slot_a_book'>$value</a> ";
}
else
{
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
}
Is it possible to change this code into function without using a loop?
$start = 80;
for ($i = 1; $i <= 10; $i++) {
$start = $start * 1.5;
echo "level ".$i.": ".$start."<br>";
}
function generate($start, $level){
// some code
return $start;
}
For level 1 you have:
$start = $start * 1.5;
For level 2 $start is result from level 1, so:
$start = ($start * 1.5) * 1.5;
This same as
$start = $start * 1.5 * 1.5;
And can be simplified to
$start = $start * pow(1.5, $level);
In the end your function should look like:
function generate($start, $level){
return $start * pow(1.5, $level);
}
if you want to get the same result(include the level print to screen) you can use this code:
function generate2($start, $from,$to){
if($from==$to+1)
return 1.5;
$tmp=$start*1.5;
echo "level ". ($from).": ".$tmp."<br>";
return 1.5*generate2($tmp,$from+1,$to);
}
Or this:
<?php
define ("MAX_LEVEL",10) ;
function generate($start, $level)
{
if($limit==0)
return 1.5;
$tmp=$start*1.5;
echo "level ". (MAX_LEVEL-$level+1).": ".$tmp."<br>";
return 1.5*generate($tmp,$level-1);
}
Here some check code:
$start = 80;//<=================your code
for ($i = 1; $i <= 10; $i++) {
$start = $start * 1.5;
echo "level ".$i.": ".$start."<br>";
}
echo"---------------------------- <br>";
generate(80,10);//<====================my code
echo"---------------------------- <br>";
generate2(80,1,10);
?>
if you not need the prints you can use very simple function:
function generate($start, $level){
return $start * pow(1.5, $level);
}
Here you go, a solution without "visible" loop:
generate(80,10);
function generate($start, $level){
$i=1; // Just a var
$array = array_fill(0, $level, $start); // create an array with $level elements, with value $start
array_map(function($v)use(&$i){ // Loop through the array and use $i
echo "Level $i: ".(array_product(array($v, pow(1.5, $i++))))."<br>"; // Some basic math and output
}, $array);
}
Online demo
Note that you'll need PHP 5.3+ since this function is using an anonymous function
Or, if you need just to output $start:
function generate($start, $limit)
{
$start = $start * 1.5;
echo $start."<br>";
if($limit>1)
return(generate($start,$limit-1));
}
generate(80,10);
My question - how to properly echo $level, without third parameter (0, in this case which should be incremented, no decremented:))? :)
EDIT: I would like to know better solution which will do the same, with two args:
function generate2($start, $limit,$base)
{
$start = $start * 1.5;
echo "level ".$base.": ".$start."<br>";
if($base<$limit)
return(generate2($start,$limit,$base+1));
}
generate2(80,10,1);
And final edit:
function generate($start, $limit,$i=0)
{
$i++;
$start = $start * 1.5;
echo "level ".$i.": ".$start."<br>";
if($limit>1)
{
return(generate($start,$limit-1,$i));
}
}
generate(80,10);
as answer to my self. :) Please test it (before down votes:)), and let me know about issues... Oh, i see - OP wants just 1 result, LOL...
Question wasn't clear to me (and not just to me, it seems) :)