This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate DateDiff in SQL in Days:Hours:Mins:Seconds format
I have this MySql query:
SELECT DATEDIFF(MAX(hit_date), MIN(hit_date)) / (COUNT(hit_date) - 1) AS hitavg FROM my_table
This returns a value (average days between recorded rows from hit_date column, this column is in TIMESTAMP format, so YY:MM:DD HH:MM:SS)
Assuming that this value is 385.500 (returned from the query), how can I format in PHP this number as 385 Days, "n" Hours (where "n" is the decimal value, in this case 500)?
Thanks in advance to all!
Well, if you only want Days and Hours, then:
$value = 385.500;
$days = (int) $value;
$hours = 24 * ($value - $days);
echo "$days Days, $hours Hours";
Related
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 3 years ago.
I want to calculate total working hours of an employee for a month.The time for one session of an employee is stored in the format 'H:i:s' as as string in the database.How do I add these individual sessions to calculate total time.
The function strtotime(datetime_today) will parse to seconds relative to the difference between the current datetime and 01/01/1970
<?php
$datetime_ref_today = '0:00:00';
$ar_times[]='00:00:10';
$ar_times[]='00:01:30';
$ar_times[]='13:50:30';
$total_time = 0;
foreach($ar_times as $val)
{
$total_time += strtotime($val)-strtotime($datetime_ref_today);
echo "<br/>cumul. total_time : ".$total_time." sec";;
}
echo "<br/><br/>total_time : ".$total_time." sec";
echo "<br/>Formated total_time (H:i:s): ".gmdate("H:i:s", $total_time);
?>
This question already has answers here:
Using DateTime::diff() to return days between two dates
(2 answers)
Closed 4 years ago.
I calculate the difference of to queried dates. When the difference is lower than one month everything is correct. Here a example of a wrong result:
Query output:
$row['start'] = '2018-08-06';
$row['end'] = '2018-09-26';
Code:
$start = new DateTime($row['start']);
$end = new DateTime($row['end']);
$days = $start->diff($end)->format("%d");
Output:
$days = 20;
The DateTime difference is correct and the error only comes in when you are formatting the output. %d is the day of the month, not the total days in the DateInterval. These two datetimes are 1 month and 20 days apart. So %d only shows the 20 days part. %a should get you the total number of days.
A full guide to the format can be found here:
http://php.net/manual/en/dateinterval.format.php
This question already has answers here:
PHP: producing relative date/time from timestamps
(10 answers)
How do I compare two DateTime objects in PHP 5.2.8?
(8 answers)
Closed 7 years ago.
Here, I want to get hours between two dates.
If I subtract time between two dates, I want 23 hours in a result.
My field's datatype is as datetime.
My code is like,
$usr_check_login = $this -> applib->get_any_field('users_attendance',array('user_id'=>$this->session->userdata('user_id'),'clock_out'=>'0000-00-00 00:00:00'),'clock_in');
$cur_time = date('d-m-Y H:i:s');
$clk_time = date('d-m-Y H:i:s',strtotime($usr_check_login));
$res = $cur_time - $clk_time;
$hours = $res / ( 60 * 60 );
echo $hours."<br>";
echo $cur_time."</br>";
echo $clk_time."</br>";
I am getting result like,
0.00027777777777778
23-01-2016 11:29:19
22-01-2016 11:02:39
Here I want result as 24 hours as the dates are changed.
What can be the solution?
This question already has answers here:
MySQL week calculation between two dates
(2 answers)
Closed 10 years ago.
I am creating a Top 30 Music Chart..
Hw do I get the number of weeks between two mysql formatted datetimes? like
From: 2013-01-15 11:41:14
Current Datetime: 2013-02-25 13:41:14
Using SQL is better, but in php it'll be something like this:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
Subtract the times using strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
You can just chunk down the seconds with "remainder" and find out the minutes (and it's remaining seconds) and so on until you hit the time-distance you want.
does the WEEK() function helps you in your query ?
I don't know Mysql very well but lets assume the following :
select WEEK(date1-date2) from mytable where name='Ken';
You can use strtotime() method, then count difference between current time and time you have in your MySQL database.
Try DATEDIFF(date1, date2) function in mysql.
DATEDIFF() will return you the no of date between two given date. Hope you can easily get the weeks from output.
I'm trying to calculate an average interval between TIMESTAMP (YY-MM-DD HH:MM:SS) records in a column (hit_date) from a table.
I did this in MySql:
SELECT DATEDIFF(MAX(hit_date), MIN(hit_date)) / (COUNT(hit_date) - 1) AS hitavg FROM my_table
This returns a value, ie 135.50.
Then, I did this in PHP to show results:
$value = ($res_from_mysql_query);
$days = (int) $value;
$hours = 24 * ($value - $days);
echo "$days Days, $hours Hours";
and my result is:
135 Days, 0 Hours.
But this is not the correct result... what's wrong?
Have I to use TIMEDIFF in MySql? If yes, I got a total different value... so, how can I implement my PHP script?
How can I correctly show Days and Hours for this interval?
Please, help me to improve this, any help would be really appreciated!
what about like that code:
SELECT TIMESTAMPDIFF(HOUR, MIN(hit_date), MAX(hit_date)) / (COUNT(*)-1) AS hitavg FROM my_table
TIMESTAMPDIFF(UNIT, DATETIMEEXP1, DATETIMEEXP2) returns the difference in HOURS between DATETIMEEXP1 and DATETIMEEXP2.. for each "select" the query finds the date of first and last hit (visit) and total visit's count, then calcute arithmetic average.
Then in PHP is easy way to display the difference...
Maybe you should look at these MySQL ref:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff