how to calculate time difference between two time [duplicate] - php

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
how to calculate the minute difference for 2 values:-
strtotime("-15 minutes");
strtotime("now");
i have used below code:-
$diff = floor(strtotime("now") - strtotime("-15 minutes")/3600 );
but it gives 5 days of difference.
please guide me where I am wrong.

First of all you are getting hour diff with division to 3600. 15 minutes evaluates to 0 when floored. Also you are only dividing the second variable not the first with those brackets.
this should work:
$diff=floor((strtotime("now") - strtotime("-15 minutes"))/60);
echo $diff;// outputs 15

Related

Total Days between two dates [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 months ago.
i am beginner at php. I want to calculate exact total number of days between two dates using php, mysql and want to show it in html form. I tried datediff but it doesn't works as it gives , diffrence not total number of days.
Date fm - 10-10-22;
Date to - 20-10-22;
Total Days - 11
add + 1 to date_diff the output will 11 be like :
<?php
$d1=date_create("2022-10-10");
$d2=date_create("2022-10-20");
$diff=date_diff($d1,$d2);
echo $result = 1 + $diff->format("%R%a days");
?>
php has inbuilt DateTime functions to get the difference. Below outputs 10 day difference.
$date = new DateTime('2022-10-20');
$next_date = new DateTime('2022-10-30');
echo $date->diff($next_date)->days;

Minutes used from time difference even fraction of a minute in php [duplicate]

This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 4 years ago.
I have 2 outputs from following code:
$start_time = $dt->format('Y-m-d H:i:s');
$end_time = $dt->format('Y-m-d H:i:s');
start_time - 2018-09-27 17:42:01
end_time - 2018-09-27 17:42:16
How can i get the difference in minutes, even if the time diff is below 1 minute, like example 0.5 minutes (30 seconds).
I require only minutes value, even if it is part of a minute. How is it possible in php using time functions?
Thanks in advance.
$differenceInMinutes = (strtotime($end_time) - strtotime($start_time))/60
This converts your 2 dates into a unix time first by using strtotime() (PHP documentation). The unix time is always in seconds. Then when we subtract them from each other we get the difference in seconds between the two dates. To get from seconds to minutes you can of course divide by 60.

Count hours, minutes, and seconds between two dates in PHP? [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 6 years ago.
I want to count how many hours, minutes and seconds between two dates.
For example: Date 1: 01-01-1990 12:00 and 01-02-1990 13:00
Now, how many hours, minutes, and second from date 1 to date 2 using php?
just use strtotime() on your dates to their timestampz . and then subtract them to have the difference in the number of seconds. it's easy to convert from there

How to calculate days between 2 month? [duplicate]

This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Finding the number of days between two dates
(34 answers)
Closed 8 years ago.
I want to calculate days between 2 month.1 date is set that is 01-03-2014 and second date will be given by user.This may be any date for example 14-05-2014.Now i want to calculate days between these 2 dates?
Thanks
You can use DateTime for this, and do a simply calculation. See DateTime:diff
<?php
$start= new DateTime('14-05-2014');
$end= new DateTime('01-03-2014');
$interval = $start->diff($end);
echo $interval->format("%a total days"); //Output: 74 total days
https://eval.in/236630

Subtract seconds from a specific given time PHP [duplicate]

This question already has answers here:
Subtract one second from a given time
(2 answers)
Closed 8 years ago.
I writing a code for subtract seconds from a time using php. i have date which assigned to variable , i need to subtract seconds from that date.
$date="2014-03-16 17:40:27";
echo date("Y-m-d H:i:s", strtotime($date) - strtotime("-600 seconds"));
but this gives me dates on 1970S, i search everhere and didn't found a answer which matched for my question. can anyone help me to fix this little code
strtotime() gives you a timestamp in seconds. Don't make another timestamp to subtract from it, just take 600 from it:
echo date("Y-m-d H:i:s", strtotime($date) - 600);
//2014-03-16 17:30:27

Categories