How to find the duration of two time interval in php? - php

i have a Mysql table shift_def in the following format.
+-------------------------------------------------------------------------------+
| id | name | start_time | end_time | description | break |
|-------------------------------------------------------------------------------|
| 101 | Shift1 | 01:03:55 | 06:00:55 | Shift 1 | 1 |
|-------------------------------------------------------------------------------|
| 102 | Shift2 | 06:03:55 | 01:00:55 | Shift 2 | 3 |
+-------------------------------------------------------------------------------+
to find the total shift duration. i perform the following process in my php.
$shift_time = mysql_query("select start_time, end_time from rpt_shift_def where name ='Shif1'")or die(mysql_error());
while ($row = mysql_fetch_assoc($shift_time))
{
$Total_shift_time = strtotime($row['end_time']) - strtotime($row['start_time']);
$hours=floor($Total_shift_time/3600);
$Total_shift_time-=$hours*3600;
$minutes=floor($Total_shift_time/60);
$Total_shift_time-=$minutes*60;
$seconds=$Total_shift_time;
$Total_shift_time=$hours.":".$minutes.":".$seconds;*/
echo "$Total_shift_time";
echo "</br>";
}
the output is
4:57:0
when i try the same for shift 2 the expected output is
18:57:0
but the output is
-6:57:0
the negative value is because end_time of the shift 2 is less than start_time.
how to solve this problem.
can anyone help me out.

If end time is less than start time, add 24 hours (the reason being that the shift ended on the following day).
Since you have already calculated $Total_shift_time, you can just check if that is negative.
if( $Total_shift_time < 0 ) {
$Total_shift_time += 86400; // Number of seconds in a day
}
Err... Make sure you do this after you first calculate the shift time and before you start calculating hours, minutes and seconds...

Related

How to get data in past 30 days in MYSQL and Print Past 30 days inside a loop using a variable in php

I need to get past 30 days sales records from MYSQL database to display in a bar chart how many items sold each day. Also I need to display past 30 days in my chart x axis in my php document. I tried run this code inside a for loop. because I am running some other codes to display some data using this loop. Take a look at the following code and help me how to achieve this.
This is the table I try to get data from
Date format (YYYY-MM-DD)
Products Table
product_id | sold_by | qty | added_date | sold_date
-----------+---------+-----+-------------+-----------
3 | 12 | 7 | 2022-05-05 | 2022-07-28
3 | 12 | 7 | 2022-05-05 | 2022-07-29
3 | 12 | 7 | 2022-05-05 | 2022-07-30
3 | 12 | 1 | 2022-05-05 | 2022-07-30
3 | 12 | 2 | 2022-05-05 | 2022-07-30
6 | 22 | 4 | 2022-06-06 | 2022-07-31
8 | 11 | 6 | 2022-08-05 | 2022-07-31
Deleted Table
product_id | added_date | delete_date
-----------+-------------+-----------
3 | 2022-05-05 | 2022-07-28
3 | 2022-05-05 | 2022-07-29
3 | 2022-05-05 | 2022-07-30
3 | 2022-05-05 | 2022-07-30
3 | 2022-05-05 | 2022-07-30
6 | 2022-06-06 | 2022-07-31
8 | 2022-08-05 | 2022-07-31
As you can see for some days I have multiple records.
<?php
//for loop to run 30 times to get 30 days results
for ($i = 30; $i >= 0; $i--) {
$sold_products_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM products WHERE sold_date = CURRENT_DATE - $i"));
$deleted_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM deleted WHERE delete_date = CURRENT_DATE - $i"));
?>
{
<?php
$current_date = date("Y-m-d");
$new_date = date_create($current_date);
date_sub($new_date, date_interval_create_from_date_string("$i day"));
?>
x: '<?php echo date_format($new_date, "Y-m-d"); ?>',
a: <?php echo $deleted_count; ?>,
s: <?php echo $sold_products_count; ?>,
},
<?php
}
?>
x- X axis data in the chart
a - number of products deleted in specific date
s - number of products sold in specific date
With the above code I was able to print the x axis data. I am not sure how to get past 30 days data from the SQL database. I think the problem is in the $i part in the SQL query.
In your query, if you use INTERVAL, you can use some days for adding/reducing from the current or specific date.
For example in your case, if you always want to get the last 30 days data, you can use your WHERE like this:
sold_date >= CURDATE() - INTERVAL 30 DAY

How to determin date below todays date from db in php

I'm developing a pharmacy store project, but I have a problem of determining the total number of drugs that expired. From DB I have:
+----+----------+--------+------------+
| id | drug_nam | amount | exp |
+----+----------+--------+------------+
| 1 | M and T | 200 | 04/15/2016 |
| 2 | VIT C | 20 | 05/25/2016 |
| 3 | Pana | 10 | 01/03/2016 |
| 4 | Lonat | 1200 | 08/25/2017 |
| 5 | ProC | 100 | 05/25/2017 |
+----+----------+--------+------------+
what I need here is a line of PHP script that will count the numbers of expired drugs from DB. using <?php $d = date('m/d/Y'); ?> to determine it from DB.
I used the code below but it count only 2
<?php
$d = date('m/d/Y');
$result = mysqli_query($conn, "SELECT count(exp) FROM products where exp < $d ");
while($row = mysqli_fetch_array($result))
{
echo $row['count(exp)'];
}
You should convert your string date representation to date value if you want to filter by date but not by string.
This query should work:
SELECT count(exp) FROM products where STR_TO_DATE(exp, '%d/%m/%Y') < $d
The main drawback is mysql can't use index in this case. One of the solution is to convert your column from varchar(50) to DATETIME. In this case you can use your original query.
From your table it seems you used some sort of text or varchar for you exp column. Cause mysql date format should be like yyyy-mm-dd. Please change your exp column to date and change the below line
$d = date('m/d/Y');
to
$d = date('Y-m-d');
That should be good.

Distributing data across month

Currently we store hours in a table that has a percentage field to distribute data across the week.
eg.
| dayint | daytext | hours | percent |
+--------+---------+-------+---------+
| 1 | mon | 9 | 0.14 |
| 2 | tue | 15 | 0.23 |
| 3 | wed | 9 | 0.14 |
| 4 | thu | 9 | 0.14 |
| 5 | fri | 0 | 0.00 |
| 6 | sat | 23 | 0.35 |
We are now adding a monthly distribution as well as weekly, so to calculate this I am:
Getting number of weeks in a month.
$weeks = $days_in_month/7
Iterating through each day's percent and dividing it by number of weeks.
$percent = $day['percent'] / $weeks
If my assumption is correct, this should distribute data across the month evenly. The problem is that it is consistently about 1ish % extra for the month.
If I enter 1000, I get 1013. If I enter 200, I get 202. Maybe I am just going about it wrong mathematically, any help is appreciated.
If your percent column stores data that has been rounded and is used in your monthly calculation as opposed to displaying rounded results that are stored accurately elsewhere then you will get rounding errors.
The example below shows the results for three weeks and the tables afterwards show the results using the actual data and the rounded data.
|Hours|Actual |Rounded| |Hours|Actual |Rounded| |Hours|Actual |Rounded|
| 9|0.13846153846154| 0.14| | 10|0.15625000| 0.16| | 8|0.135593220339| 0.14|
| 15|0.23076923076923| 0.23| | 14|0.21875000| 0.22| | 13|0.220338983051| 0.22|
| 9|0.13846153846154| 0.14| | 8|0.12500000| 0.13| | 11|0.186440677966| 0.19|
| 9|0.13846153846154| 0.14| | 10|0.15625000| 0.16| | 7|0.118644067797| 0.12|
| 0|0.00000000000000| 0| | 1|0.01562500| 0.02| | 2|0.033898305085| 0.03|
| 23|0.35384615384615| 0.35| | 21|0.32812500| 0.33| | 18|0.305084745763| 0.31|
| 65|1.00000000000000| 1.00| | 64|1.00000000| 1.02| | 59|1.000000000000| 1.01|
Over the three weeks iterating with
$percent = $day['percent'] / $weeks
actual column gives the results
0.14343
0.22329
0.14997
0.13779
0.01651
0.32902
1.00000
whereas rounded column gives
0.146666667
0.223333333
0.153333333
0.14
0.016666667
0.33
1.01
Remember rounded results always go up or stay the same never downwards, so the error will always be slightly more than you expect.
I decided to change my approach and go about it like this:
Get the total hours for the month by looping through:
$total = $total + $day['hours'];
Use the stored hours instead of the percent to calculate a new percent using:
$percent = $v['hours']/$total;
This approach returns the correct data.

How to calculate date time in one field

I have a table below named transaction_detail
id_transd | id_trans | id_cust | inputtime |
1 | 1 | 1 | 2013-04-15 16:55:58 |
2 | 1 | 1 | 2013-05-15 16:55:58 |
3 | 1 | 1 | 2013-06-15 16:55:58 |
4 | 2 | 2 | 2013-06-15 16:55:58 |
I want to get the amount of the inputtime (type : datetime) which have the same id_cust. I've done with the date, but I don't know how to calculate the time. This is my sql syntax to calculate the date :
select (date(max(inputtime)) - date (min(inputtime))) as total from transaction_detail where id_cust = '$idp'
Any help will be very appreciated. Thanks before.
Have a look in this website this might help what you need.
http://www.sqlusa.com/bestpractices/datetimeconversion/
Use TIMESTAMPDIFF function to get difference between two datetime values in units you want.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
Units could be: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

sum hits for every week in mysql

I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.

Categories