Calculate average of two data fields php/mysql - php

I wonder know how to deal data format. I have 2 columns date(starting date) *date2 (end date)*. I am not familiar with php/mysql.
one my biggest clue is the data/data2 fields appears on this format ie. "1394797440". I am trying to build a dashboard with DB graphics of a ticket support. Had some features done but the data format is stressing me up. let me know if I am right.
I need to do a foreach on both fields? sorry my noob question but I am totally lost :|
mostly I use this call-> $call_date = date("m/d/y",$site_calls->call_date);
I will really appreciate any help.

This is a UNIX epoch datetime format i.e. number of seconds elapsed since 1st Jan, 1970. To take average of the two dates, it is similar to any other numeric average.
$avg_date = $site_calls->call_date + (($site_calls->call_date2 - $site_calls->call_date)/2);
$call_date = date("m/d/y",$avg_date);

The data/data2 format is most likely Unix epoch time. It is the number of seconds since Jan 1, 1970 and can be converted to an actual date and time.

Related

PHP distinguish between only-date or only-time as well as both

I am posting this optimistically after searching for an answer here on SO, and even when SO tells me my question might be closed, as I think in this case it's a valid question.
Consider a CSV file with a column containing string representing either dates, or times, or both. I want to find out after reviewing the column, just that - exactly what type of column is it, not just that it's a valid "date"?
PHP function strtotime does an amazing job of returning a unix timestamp for pretty much any date-time-ish string. But (today when I'm posting this on 10/8/2018), 3:45PM and 15:45:00 and 10/8/2018 3:45PM would all return the same unix time, though obviously the first two are times.
What is a method to determine if a string is strictly a date component, a time component, or both?
P.S. If I have to write a function myself, so far the best lead would be to look for a : in the string, which would mean there's a time component (meaning either time, or datetime). If it parses as a datetime, but with no : present, then we could assume it's a date only. But again, I am wondering if PHP has a more elegant way. Here is a "pretty good" solution:
P.P.S this function is actually a "very good" solution now thanks to #KarstenKoop's clever suggestion in comments about the 2nd parameter for strtotime:
function date_time_component($date){
if(strtotime($date) === false) return false;
if(strtotime($date, 86400) !== strtotime($date, 86400 * 2)) return 'time';
if(strstr($date, ':')) return 'datetime';
return 'date';
}
You should start by writing a set of test cases - which you could have included in your question. There are lots of different ways to write dates and a few ways to write times.
Here's some to get you started:
dates
2018-10-08
08/10/2018
10/08/2018
08-10-2018
10-08-2018
10-Oct-2018
20181008
10 Oct 2018
10th October 2018
October, 10th 2018
times
1121
11:21
11:21 AM
11:21
11:21:46
11:21:46 AM
Timezones
+00:00
+0
+0000
Europe/London
Then you have all the possible combinations of the 3 components:
date
date time
date time timezone
time
time timezone
time date
time timezone date
That gives 400 different formats (not all of which are uniquely resolvable).
(using strtotime to do the heavy lifting starts making a lot of sense). But rather than trying to parse the data, looking for specific patterns might be a better approach: are there strings of letters? a + or -? How many digits? How many consecutive digits?
You should still be starting with a list of test cases.

Inserting actual hours (not time) to MySQL

I am trying to insert actual hours not the time itself to MySQL database through form fields. So for example
$time1 = '00:00';
$time2 = '27:20';
$time3 = '00:45';
So I can retrieve the different rows and can calculate on the fly whenever require. Either through search query or even in other area of the system.
When I have tried to do addition of above three times, it is not giving the result the way I am looking for
$total = strtotime($time1) + strtotime($time2) + strtotime($time3);
echo date('H:i:s', $total);
The result
14:16:44
While it should be something like
28:05:00
I have used TIME DATATYPE in MySQL table. I may use as a TEXT but I am also concern about the error happen in user input. Where I do not have to force the user to insert the any particular format but they can either insert as below way
27.20
27:20
or
1.5
1:30
My main concern is to calculate the time, the user input can be on second priority but it would be great if can implement.
So is there anyway, idea or hint to achieve this?
date() expects the timestamp in UNIX format, i.e. seconds since January 1 1970 00:00:00 UTC (which is also the value provided by strtotime)
You're passing it the result of adding a series of amounts of time since 1 January 1970 instead of just adding up hours, so (as far as date is concerned) you're generating a random date and time, and printing only the time (try printing the date of $total and see what you get).
Since your time is stored in the database, one possibility is to let MySQL handle the time calculations itself, e.g.:
SELECT ADDTIME('00:00',ADDTIME('27:20','00:45'))
will result in "28:05:00". You can have your database fields as TIME and operate on them directly through SQL, and do the user input conversions into acceptable TIME values in PHP.
If you're only interested in the hours and minutes, why don't you just store the value as an in integer? Just multiply the hours by 60.
You can handle the conversion in PHP.
Alternatively, you can also easily use two (very small) int fields for this.

PHP comparing non A.D. dates

I need to compare Bikram Sambat dates in PHP. Lets say today in AD is 2012-08-03, the BS date today is 2069-04-19. In BS, we have total number of days in a month ranging from 29 to 32. Again, the days are not consistent in every year. Example if this year current month have 31 days, next year same month might have 30 or even 32 days.
The way i could think is, using a php date converter class (which I already have), convert given BS Dates to equivalent AD Dates and compare them. Just i want to know is there any other better way to do this without using the class i have??
By saying comparing here I mean
a) Finding days between the given date ranges.
b) Checking whether a date lies on a given two date ranges
c) And other comparisons we could do with AD dates using php Date Function
thanks any help is highly appreciated.
Generally, I would suggest converting both dates to unix timestamps (seconds since 1970-1-1 AD) which is a simple integer value for comparison.
Apparently, there is no simple logic behind BS so you will not be able to find an analytic expression that directly gets you from here to there. If you do not wish to use an external class you could write some sort of (recursive) function that starts from 1970-1-1 AD which you manually convert to BS and counts up the timestamp from there following the rules for BS.
Have a look at the source code of KashmirStamps.ca/Samvat.html
It is converting BS Dates to AD dates. You will find Javascript Code & Logic. You can easily adapt the logic to PHP code.

Merging 2 int type strings into one UNIX timestamp

I have a table with 2 rows, one of which represents date and the other time. These rows are not date format; they are int type and I can't change the original rows' type. The date entries are written as 20120306, and the time entries are written, for example, 13000 for 01:30 UTC in 5 digits, and 130000 for 13:00 UTC in 6. What I need to do is put both strings (date and time) into 1 UNIX timestamp. I can either use this in a php script or merge them into a new table, whichever works best. The problem for me is the php mysql syntax, functions and sequence for putting these 2 odd strings together into one timestamp.
If I can figure out how to do this, it would help me solve a whole mess of inaccuracies in a calendar reminder script I am trying to put together. I have tried configuring these strings into times separately in the queries, but no matter how it's filtered, the outcome is right in some circumstances and wrong in others due to the nature of the original program. If I had a PHD in PHP I would rewrite the entire program but I don't. I am a major newbie at this. So I just need to write my little PHP scripts to utilise what has already been provided. Any help in my learning quest would be appreciated.
The function strtotime() can also read compound formats.
If you take a look at that list you will notice that your format is a close match to the "XMLRPC (Compact)" listed there.
It's your date string concatenated with a "t" and the time string. So, after you expanded your time string to length 6 (you already know how to do that), you can produce such a string and strtotime() will output your Unix time stamp:
$dt = "20120306";
$tm = "130000";
$unixTime = strtotime($dt . "t" . $tm);

difference between two times in PHP

Hi All
im trying to build a simple form that the user use it to enter his leave request.
the form contains from time input field and to time input field,and these two filds will contains values in this syntax:
from time: 15:59 pm
to time: 16:59 pm
i have two questions:
1. what is the Datatype that i should use to store p.m and am in the record in mysql database, and not only the time?(i try to use Time,DateTime Date) but these datatypes only stores the time without p.m,a.m
2. what is the best way to calculate the diffrence between these two times?
Thank You
If you are comparing dates/times I find it easiest to work with a timestamp.
There are tons of date functions in PHP if it's just to output the date in the format you want have a look at date functions
The best way to store times in the database is in 24-hour time and use PHP's date function to format the time when you pull it to display it using AM or PM.
To compare two times, I suggest looking at thestrtotime function. This will return the time in UNIX fashion (seconds). You can then compare which time is greater or less than each other, or even perform basic mathematics operations on them (like determining the amount of seconds between each time and then dividing by 60 to determine minutes, etc).
Just store it as a timestamp, you can add pm/am automatically when you output:
print date('G:i a',1294239540); // prints 15:59 pm
and to get the difference in seconds just use strtotime and substract:
print (strtotime('16:59') - strtotime('15:59')); //prints 3600 (1 hour in seconds)
1) This doesn't make any sense - the database stores a representation of an exact instant in time, not an ambiguous 12 hour format with no am/pm. Regardless of how the db is storing it, you can just calculate the am or pm:
<?php
$am_pm = date('a', $timestamp);
?>
or even better, in mysql using your time field:
SELECT DATE_FORMAT(date_field, '%p')
2) For this you can use either php's date_diff, or in mysql:
SELECT DATEDIFF(date_one, date_two)
question 1
stored as time, which use ISO 8601 (hh:mm:ss)
question 2
use timediff, like
select timediff(cast('13:59:29' as time), cast('10:20:00' as time));
>> 03:39:29
To display the AM/PM
select time_format(cast('13:59:29' as time), '%r');
Storing am and pm when you're using 24 format is redundant. Anything between 11:59 and 00:00 is automatically pm. DateTime/timestamp should be more than sufficient to do what you're trying to accomplish. Then you can convert it using strftime back to 12-hr with am/pm.

Categories