How to ordinalize a day using php? - php

Using PHP I want to show the day of the month in ordinal way like ::
1 first
2 second
3 third
4 fourth
5 fifth
6 sixth
7 seventh
8 eighth
9 ninth
10 tenth
11 eleventh
12 twelfth
13 thirteenth
14 fourteenth
15 fifteenth
16 sixteenth
17 seventeenth
18 eighteenth
19 nineteenth
20 twentieth
21 twenty-first
22 twenty-second
23 twenty-third
24 twenty-fourth
25 twenty-fifth
26 twenty-sixth
27 twenty-seventh
28 twenty-eighth
29 twenty-ninth
30 thirtieth
31 thirty-first
e.g.
if date is 22-Feb it should show : twenty-second Feb
Thanks in advance....

Well, you already have your "array" there, just put it in code:
$ordinals = array(
1=>"first",
2=>"second",
// ...
);
Then you can access the array:
echo $ordinals[date("d")];

Related

How can I get data from 2 table based on specific condition

In MySQL database, I have 3 table called supplier, vehicle, and assigned table.
vehicle table:
vid vehicleName noOfSeat sid
========================================
13 x 10 28
14 x 8 28
15 x 12 28
16 x 14 29
17 x 4 29
18 x 8 30
assigned table
asgid bid sid vid did seatBooked assigned_start assigned_end
============================================================================
56 15 28 13 17 3 06/01/2018 02:01 06/01/2018 04:02
57 15 28 14 15 2 06/01/2018 02:01 06/01/2018 04:02
58 15 28 15 16 3 06/01/2018 02:01 06/01/2018 04:02
In a reservation page, I need to assign a vehicle to a selected date which is 06/01/2018 02:01 To 06/01/2018 04:02
Now I want to show all vehicle of a selected supplier as well as the number of seat from the vehicle table
But the condition is:
Those vehicles will be shown which has the number of seats available for my selected date: 6/01/2018 02:01 To 06/01/2018 04:02
For example:
It's must be shown following vehicle with the number of seats:
vid Mp pf Seat available
===========================
13 7 ( why 7? because on my selected date 3 seats booked)
14 6 ( why 6? because on my selected date 2 seats booked)
15 9 ( why 9? because on my selected date 3 seats booked)
16 14
17 4
18 8
Its something like bus ticket system.
My Current Query:
Currently, I am using the following query but it just removes those vid which is in my selected date but it's incorrect because as you can see that on my selected date few seats are available!!!
// my selected date: From = '$timestart' and To = '$timeend'
foreach ($sid as $key => $value) {
$vehicle->rowQuery("select * from vehicle where vid not in (
select vid from assigned where assigned_start BETWEEN '$timestart' AND '$timeend' AND assigned_end BETWEEN '$timestart' AND '$timeend' ) AND sid = $value ");
}
I can't imagine how can I solve it: =(
Your help is highly appreciated.
You can do something like this:
select vehicle.*, (noOfSeat -
(select sum(seatBooked)
from assigned
where sid = 28
and (assigned_start between '2018-01-06 02:01' and '2018-01-06 04:02' )
and (assigned_end between '2018-01-06 02:01' and '2018-01-06 04:02')
)) as seatsAvailable
from vehicle
having seatsAvailable > 0
http://sqlfiddle.com/#!9/c6bcb6/15

Laravel - Efficient Way to Query Monthly Data per Category

I'm working on a project that requires me to generate a report similar to this:
Category Total Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
-----------------------------------------------------------------------
Category 1 97 10 10 10 10 10 5 10 10 7 5 5 5
Category 2 120 10 10 10 10 10 10 20 10 5 5 10 10
Category 3 5 0 0 0 3 0 2 0 0 0 0 0 0
This report needs to be generated from data similar to this:
id category_id type year month amount
----------------------------------------------------------
1 1 16 2016 1 4
2 1 23 2016 1 6
3 1 76 2016 2 3
4 1 27 2016 2 3
5 1 18 2016 2 4
In the report, each month's number is simply the sum of the amount for that particular category, year, and month. I can do this just fine by querying the database for each month and for each category, but this type of approach doesn't scale very well if I need to generate a report with about a hundred categories because it would take over 1,200 queries (100 categories X 12 months)!
Any suggestions of how to make this more efficient?
Thanks!
The solution is grouping by two columns.I added the necessary code to format the results as you want.
Item::where('year', $year)
->groupBy('month', 'category_id')
->selectRaw('sum(amount) as sum, month, category_id')
->orderBy('category_id')
->orderBy('month')
->get()
->groupBy('category_id')
->map(function($item){
$item = $item->pluck('sum', 'month');
$item['total'] = $item->sum();
return $item;
});
Is this what you're looking for?
Category::where('year', 2016)
->select('month', 'year', DB::raw('SUM(amount) as amountTotal'))
->groupBy('month', 'year')
->get();

How to count data group by per 6 hours interval in every day in timestamp from database in PHP?

this is my query:
$sql = "SELECT DATE( pm_timestamp ) AS d, HOUR( pm_timestamp ) AS h,
COUNT( * ) AS c FROM pm_impr_tracker WHERE campaign_id = '$camp_id' GROUP BY h,d order by `pm_timestamp`";
this gives per hours data but i want per 6 hours.
my results:
d h c
2015-08-23 6 6
2015-08-23 7 11
2015-08-23 8 26
2015-08-23 9 57
2015-08-23 10 36
2015-08-23 11 19
2015-08-23 12 21
2015-08-23 13 18
2015-08-23 14 18
2015-08-23 15 17
2015-08-23 16 107
2015-08-23 17 55
2015-08-24 8 26
2015-08-24 9 57
2015-08-24 10 36
2015-08-24 11 19
2015-08-24 12 21
2015-08-24 13 18
2015-08-24 14 18
The code you posted groups the records by hour. If you need them grouped by intervals of 6 hours then all you have to do is to make h be the number of such a 6 hours group in the day. For example, h = 0 is the group of hours 0..5 (i.e. HOUR(pm_timestamp) between 0 and 5), h = 1 is for hours 6..11 and so on.
This can be easily achieved if you divide the hour by 6 and ignore the remainder. That's what the MySQL DIV operator does for a living.
The query is:
SELECT DATE(pm_timestamp) AS d,
HOUR(pm_timestamp) DIV 6 AS h,
COUNT(*) AS c
FROM pm_impr_tracker
WHERE campaign_id = '$camp_id'
GROUP BY h, d

Nested for and do while loop

How do i create a nested loop that will output the following numbers?
each round the inner loop increase from 1 to 5, 5 to 10 and so on.
from i = 0 to 5
inner loop:
result: 1 2 3 4 5
result: 6 7 8 9 1 0
result: 11 12 13 14 15
result: 16 17 18 19 20
next
for($i=0;$i<50;$i++)
{
$s = $i +5;
echo $s;
}
unless you have to use an inner do...while loop, this will work:
<?php
$max = 5;
for($i=0;$i<$max;$i++){
for($j=1;$j<=$max;$j++){
echo str_pad(($i*$max)+$j,4);
}
echo "\r\n";
}
output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
I used str_pad() just to make the columns more uniform

CodeIgniter table class loops through db records 3 times - why?

This is my controller code (using Colin Williams' Template class):
$this->load->library('table');
$table['records'] = $this->db->get_where('data', array('category_1' => 'weight'));
$this->template->write_view('content', 'vw/weight_vw', $table, TRUE);
And my view code:
<div class="grid_16">
<?php echo $this->table->generate($records); ?>
</div>
Look at what I get
2 1 29 2011-01-01 10 weight
4 1 29 2010-11-03 11 weight
5 1 29 2011-05-02 10 weight
6 1 42 2011-07-11 23 weight // the database only has records up to here
2 1 29 2011-01-01 10 weight // from here on it's repeated twice
4 1 29 2010-11-03 11 weight
5 1 29 2011-05-02 10 weight
6 1 42 2011-07-11 23 weight
2 1 29 2011-01-01 10 weight
4 1 29 2010-11-03 11 weight
5 1 29 2011-05-02 10 weight
6 1 42 2011-07-11 23 weight
Any ideas why this is happening? None of my code is inside a loop or anything like that.
Use
$this->output->enable_profiler()
to see the db queries.
Take the query and run it against your db directly.
It's most likey you have a bad join in your database model.

Categories