What's the best way to retrieve start_time and end_time times from a mysql db using php and add them together to get a total of hours and minutes? An example military time in my db is: 18:35:19 to 22:05:14. Is it easier with standard or military time? Thanks in advance.
Carlos,
Some further information such format the data is stored within mysql would have been helpful, as there are certain mysql queries to help with this. But seeing as you've tagged PHP, I would do the following in php:
convert the time to unixtimestamp using something like mktime (http://php.net/manual/en/function.mktime.php)
$unixtimestamp_start = mktime($hour,$minute,$second,$month,$day,$year);
$unixtimestamp_finish = mktime($hour,$minute,$second,$month,$day,$year);
minus the start time from the finish time.
$timeDiffSeconds = $unixtimestamp_finish - $unixtimestamp_start;
You now have the amount of seconds between finish and start.
you can divide that by 60 to have minutes
$timeDiffMins = $timeDiffSeconds/60;
you can divide that by 60 again to have hours.
$timeDiffHrs = $timediffMin/60;
Related
What i want is simple, i have timestamp in my mysql database that records date and time data registers. What i want is to calculate the timediff between timestamp and current time then subtract from 3hours to know time remaining in hh:mm:ss format, please someone help out.
You should use the following part in your query:
SELECT TIME_FORMAT(SEC_TO_TIME(AVG(TIMESTAMPDIFF(SECOND, NOW(), column_with_date_to_compare))), '%H:%i')
You must skip the AVG part if you do not want averages but a result per row (or you have only one row to check) (or use GROUP BY [something])
The part 'then subtract from 3hours', I don't understand. You only want to show the records where the time is less than 3 hours? Just use WHERE TIME(record_to_check) > (NOW() - 10800).
If you want to add, calculate or do other things to influence the result, just do so before SEC_TO_TIME, you can do the math (with seconds) there.
I've interpretted your question as how to calculate the time remaining in a 3 hour period starting at a datetime stored in a DB, to be displayed in HH:MM:SS format...
I generally find it easier to manipulate dates / times in php rather than wihtin an SQL query. So my approach would be to:
read strings from the database
convert them into unix timestamps (ie
number of seconds elapsed since a given epoch)
manipulate them mathematically (ie add on 3 hours and subtract the curent time)
lastly convert the result back into a date / time in your chosen
format.
Assuming $start_str has been read from your DB
$start_str = '08-03-2017 11:10:00';
$start_ts = strtotime("$start_str");
$end_ts = $start_ts + (3 * 60 * 60);
$now_ts = strtotime('NOW');
$remaining_ts = $end_ts - $now_ts;
$remaining_str = ($remaining_ts > 0)? sprintf('%02d:%02d:%02d', ($remaining_ts/3600),($remaining_ts/60%60), $remaining_ts%60) : "None, time's up";
echo ($start_str.'|'.$start_ts.'|'.$end_ts.'|'.$now_ts.'|'.$remaining_ts.'|'.$remaining_str);
Examples...
08-03-2017 11:10:00|1488971400|1488982200|1488983863|-1663|None, time's up
08-03-2017 14:30:00|1488983400|1488994200|1488983982|10218|02:50:18
Obviously in reality you're only interested in the last field, but the others show you what you're playing with during the process.
So, I have an issue that I'm trying to wrap my head around. Essentially I have a database that shows when computers were last connected to the database. I am supposed to calculate the uptime of the computers as a percentage (hours connected since first connect / total hours since first connect.)
All I have is a single DateTime field in a mysql database. At that point I could do a "Select TIME from database where ID="1" order by TIME asc limit 1" to get the oldest date, but how would I get the number of hours since that DateTime object?
The desired output would be something like "95%" -- which could be 19 / 20 or something.
Thanks for the help!
use
timestampdiff(HOUR,NOW(),OLDESTTIME) as uphours
that would give you difference in hours between two datetime
Convert your DateTime to a timestamp, subtract the oldest from the latest and divide the result by 60 and by 60 again:
(LatestTime-OldestTime)/60/60
Should give you the number of hours between them.
First, an entry in the database:
I have an input form that writes start date, start and end times (in hour and minute) of working days plus lunch break in minutes (in the example dato=date, modetime=start hour, modeminut=start minute, fyrtime=end hour, fyrminut=end minute). I need to do several calculations:
First calculate the date, start hour and minute into the datetime field modetid.
The do a similar calculation with the end hours and minutes, but move the date up one day if end hours is less than start hour (lets call it fyrtid)
And finally calculate the difference between fyrtid and modetid minus the pause minutes.
Can it be done directly and automatically in the database (if yes, how) or do I need some PHP to do it (and again, if yes, how)?
I know its a tall order but I have not been able to find much information on date/time calculations that made much sense on my low level of knowledge. Any link to a comprehensive guide on date/time calculation in MySQL or PHP would also be greatly welcomed.
I suggest you to work by php function time() it's based on unix timestamp ( like UNIX_TIMESTAMP() in Mysql ) unix time is like this : 1307387678.
Use a calender in your form for start time also for your end time.
put a facility what clients could select time of day ( hour and minutes ) then covert those fileds by strtotime() to unix timestamp like following code ( set date format to mm/dd/yyyy ) :
$startdate = strtotime ( "$_POST['calender_start'] $_POST['hour_start']:$_POST['minutes_start']" );
$enddate = strtotime ( "$_POST['calender_end'] $_POST['hour_end']:$_POST['minutes_end']" );
Change your db table fields to : cpr,startDate,endDate,pause,basked,.....
it's so flexible.
while you want to fetch special recorde you could fetch rows by this sql :
SELECT ...... FROM [TABLE_NAME] WHERE [VALUE] BETWEEN startDate AND endDate
I hope it be usefull for you
How to get how many hour minute seconds laps on message on php mysql?
like
23 seconds ago
1 minute ago
is this possible?
A really simple way is using the jQuery plugin timeago http://timeago.yarp.com/
The neat thing about it is, that it updates the entries while viewing the page :)
In db table create field time INT(10). Insert in this filed php time().
In script check diff:
$diff = time() - $time_from_db;
echo $diff . 'seconds ago'.
If you did want to have the data fed from the database you could write SQL like this:
SELECT FLOOR((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(your_date)) / 86400) as days FROM your_table
(MySQL example)
You could should then repeat for hours, minutes etc. subtracting the reminder.
From your result you would have $result['days'] available.
This said I would go for the timeago solution mentioned by Tim. But it is hard to say not knowing your application.
i have a php page .
Here i will get the start time as query string (eg $startTime= '8/27/2010 1:15:17 PM')
Now i need to find out the total seconds between the current time and started time
How can i find out this in php
echotime()-strtotime($startTime);