I want result in hours between two datetime [duplicate] - 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?

Related

How can we convert H:i:s Time from sql table to second? [duplicate]

This question already has answers here:
function for converting time to number of seconds
(7 answers)
Convert time in HH:MM:SS format to seconds only?
(10 answers)
How to convert time from hh:mm:ss.mmm to seconds and milliseconds mysql
(1 answer)
Closed 7 months ago.
I have a time of H:i:s like 01:51:36 in my SQL table, how can we convert it to like the sum of time in seconds 01:51:36 = 110160 Seconds
My code:
SELECT * FROM tbl WHERE name = Time
while ($row = mysqli_fetch_array($result)) {
$Duration = date("%s seconds');", strtotime($row["audio_duration"]));
echo $Duration;
}
Results = %31 31UTC2022-08-08T00:02:31+00:00202280831');

month with leading 0 getting wrong result from createfromformat [duplicate]

This question already has answers here:
DateTime converts wrong when system time is 30 March
(2 answers)
Closed 1 year ago.
I want to convert this date 2021-02 into 02-2021, so I did:
echo DateTime::createFromFormat('Y-m', '2021-02')->format('m/Y');
but this return 03/2021
and the correct result is 02/2021
you have to specify the day:
echo DateTime::createFromFormat('Y-m-d', '2021-02-someday')->format('m/Y');
for example:
echo DateTime::createFromFormat('Y-m-d', '2021-02-01')->format('m/Y');

How can I calcualte Age in Years, Months and Days [duplicate]

This question already has answers here:
Calculate months, years and days between two given dates as timestamp [duplicate]
(4 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 2 years ago.
Here is my code:
<?php
$userDob = $result['DoB'];
//Create a DateTime object using the user's date of birth.
$dob = new DateTime($userDob);
//We need to compare the user's date of birth with today's date.
$now = new DateTime();
//Calculate the time difference between the two dates.
$difference = $now->diff($dob);
//Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
//Print it out.
echo $age;
?>
I want to output something like 50 Years, 6 Months, 20 Days but I don't know how to exactly put it as this method is only outputting number of years only.
Yes php has something built in for that :)
$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');

PHP DateTime diff returns wrong difference [duplicate]

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

How to calculate time difference in php or laravel [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
i have two time like total worked time and total duty time. eg.
$dutyTime= 182:00:00; ("H:i:s")
$workedTime= 178:10:28; ("H:i:s")
my question is that how can i get difference between this two time;
Expected Output
$duration=03:49:32; ("H:i:s")
i tried strtotime() but i takes upto 24hrs.
<?php
$start = date_create('21:21:05'); //start time
$end = date_create('10:20:00'); // end time
$diff = date_diff( $start, $end );
$date= $diff->h.':'.$diff->i.':'.$diff->s;
echo date("H:i:s",strtotime($date));
?>

Categories