Format outputting time in the Yii2 - yii2

\Yii::$app->formatter->asDuration($timePeriod)
this code gets from database data, 15 days, 9 hours, 48 minutes, 3 seconds ago I get. How to format this code? To cut days and seconds, what method will help?

Related

Get the time difference in minutes between two timestamps (Mysql)

Hi I tried the following:
timediff(`date2`, `date1`) which gives me the time difference but if the days are different it does not account for the day difference. so if date2 is yesterday and date1 is today it will provide something like this: 00:00:07
I saw the timestampdiff function a couple of times, but I am using MySQL in Domo, and this function is not offered there. Any other suggestion how to get the time difference between two timestamps (where days are different) in minutes?
SELECT TIMESTAMPDIFF(MINUTE, '2020-01-07T12:17:03', '2020-01-06T13:14:02')
returns -1383
you can change MINUTE to SECOND or other formats as well.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff
edit: sorry, just read that the function is not available in your environment...
therefore I suggets to convert the datetimes to seconds and work your way up from there, e.g.
SELECT (UNIX_TIMESTAMP('2020-01-07T12:17:03') - UNIX_TIMESTAMP('2020-01-06T13:14:02')) / 60

How to do repeat on all non-numeric characters using the preceding number in the string

I am trying to get data from a rostering system. I am not sure if this is just a bad design or if that is common set up but the full day roster is broken down into 15 minute intervals starting at midnight and this is all stored in one field in the below format.
32,20C2L16C
The "," marks no activity so 32/4 means that this person started at 8am, then worked for 5 hours before taking 30 minute break for lunch (L) and then doing another 4 hours of work. I am trying to calculate the hours worked in a separate column - it would give me 9 hours here.
I was thinking that one way to solve it would be to do repeat on all the non-numeric characters to get this:
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,CCCCCCCCCCCCCCCCCCCCLLCCCCCCCCCCCCCCCC
And then to simply count all Cs and divide by 4. Is it possible to do repeat like this in MySQL? Would there be a better way that I may be missing?

msyql ROUNDING for FLAT RATE charging

Need help with rounding values, I know ROUND() exists but it's not quite what I needed.
So, I'm calculating call rates per minute, in which in a flat rate basis, any call goes more than 60seconds will be charged for the next minute.
My query goes SEC_TO_TIME(billedsec) to get the minute equivalent.
I have 2 issues:
How to round off to a full minute for values that goes more than a minute say, if 1min and 15sec (00:01:15) should be 2mins or 13seconds must be a full minute.
How to calculate TIME format to an int or float to get the actual rate
e.g 00:01:00*rate? Is it straightfoward approach?
Thanks guys
You can use
FLOOR((billedsec+59)/60)
to get the time rounded up to the next minute. 13 seconds will go to 72/60=1 minute, 60 seconds will go to 119/60=1 minute, 61 seconds will go to 120/60=2 minutes, 75 seconds will go to 134/60=2 minutes etc.

Database setup for activity logging

I'm currently working on an application, which requires detection of activity within the past 12 hours (divided into 10 minute intervals, i.e. 6 times an hour). My initial thought has been to do a cronjob every 10 minutes to detect and accumulate activity level (rows) from a table within the past 10 minutes, and then insert these into a new table, which collects and updates the activity level for e.g. 1.20 (from current timestamp).
Though I'm struggling in figuring out the logic of "pushing" (in need of a better word) all other values to next value within the table. E.g. 1 hour and 20 minutes ago, should then be "pushed" to 1 hour and 30 minutes ago etc.
I realize that my thoughts as to the setup is limited to my understanding of PHP/MySQL and use of same, but am open to other setups such as NodeJS/MongoDB if that seems more flexible and feasible. The output should be a JSON file, showing the activity level for each hour divided into 10 minutes for the past 12 hours.
Would love some thoughts/feedback as to approach and way to handle this. Thanks a bunch in advance.

Best way to store time interval values in MySQL?

Stupid easy problem, but I haven't been able to find an elegant solution. I want to store time intervals in a MySQL columns, for instance:
1:40 (for one hour, 40 minutes)
0:30 (30 minutes).
Then be able to run queries, summing them. Right now I store them as INT values (1.40), but I have to manually do the addition (unless I'm missing an easier way).
The TIME column type only stores upto 900 hours (about, I think), so that's (almost) useless for me since I am tracking upwards of hundreds of thousands of hours (I store one field with a summation of many different entries).
Thanks!
store just the minutes, so 1:40 gets stored as 100. this makes for easy addition: 100 + 30 = 130. when you display, do the math to convert back to hours:minutes. 130 minutes -> 2:10.
I would simply store them in an INT field as seconds or minutes (or whatever the lowest time value you are working with).
As an example, you want to store the following time value 1 hr 34 min 25 seconds:
That is 5665 seconds. So just store the value as 5665 in an INT field
Later, lets say you want to store the time value 56 min 7 seconds:
That's 3367 seconds.
Sum up everything later: 5665 + 3367 = 9032 seconds
Convert that back to hours, minutes, seconds, you get 2 hrs 30 min 32 seconds. Yes, you have to do the math to make the conversion, but it's a very simple calculation and it's not at all machine intensive since you are only doing it once, either before you store the INT or once, after you retrieve the INT.
You can definitely use an int field for this, especially since MySQL provides the DATE_ADD and DATE_SUB functions for using integer units of time alongside date and datetime types in date artihmetic.
For example, if you have a datetime column eventDateTime and you have a duration column durationMinutes you can calculate the ending datetime using:
DATE_ADD(eventDateTime,INTERVAL durationMinutes MINUTE)
I know this is not the kind of time interval you are talking about, when I when I searched around this is about the only relevant SO question I found, so to help out other lost souls like myself I thought I would post what I am doing now.
I am storing a Subscription length, which rather than precise values like "number of seconds" or even minutes is either Days or Months. So in my case I have a "duration" INT and a "duration_unit" ENUM of ('days','months').
So for a "6 month" subscription rather than trying to calculate how many minutes are in a 6 months (which varies depending on which 6 months you are talking about) I just store "6" and "months".
With PHP's mktime() method it is easy and more accurate to calculate time intervals of +6 months.
If you want to store them as hours, and thus keep the integer small, you can take the number of minutes and divide by 60.
So 1:40 becomes 100/60 = 1.66 , :30 becomes 30/60 = .5. Now you can just add them up normally:
1.66 + .5 = 2.16
and then you have the number of hours as the whole, and the decimal part is the number of minutes over 60, so that would be
.16 * 60 = 10
so it's 2:10
Your next best option is to see if MySQL can do base60.