DateTime Calculation Minutes [duplicate] - php

This question already has answers here:
Calculate number of hours between 2 dates in PHP
(17 answers)
Closed 9 years ago.
Is there any recommendation of datetime calculation?
I have been going toward UNIX timestamp and diff calculation method
but both of it don't really provide a better way to complete my task.
Here is the sample of datetime I have.
2013-09-07 21:12
I wish to calculate the duration between the 2 datetime.
Lastly display the total of minutes duration for the 2 datetime.
Most of the calculation work to display for year, months, days, hours, minutes, seconds.
I need some method to find out the duration between 2 times and convert the duration into total of minutes.
Anyone?

Use function mktime to get the UNIX timestamp of the two dates, then take the bigger one and subtract the smaller one. You'll get another value, if you divide that value by 60, you'll get how many minutes are inbetween the two dates.

Related

Return current date + 7 days in timestamp form [duplicate]

This question already has answers here:
Return current date plus 7 days
(10 answers)
Closed 2 years ago.
So I have the below property where I'm passing in a time()-86000 value, but what would be the best call to generate a timestamp from the time the class method executes and then adds 7 days to that.
Here is what I got:
$profile->set_picture_expiration(time()-86000)
Would using time()-86000 be the right call? I'd like to write it to the DB in timestamp format from the current time + 7 days.
First of all, if you need +7 days, why are you using minus?
Next, 7 days are 7*24*60*60 = 604800 seconds, not 86000.
Finally, the easiest way to get the timestamp for such relative dates is using the strtotime function. In your specific case it would be strtotime('+7 days').
$profile->set_picture_expiration(strtotime('+7 days'));

How to divide time between particular minutes in php [duplicate]

This question already has answers here:
display hours in half hour range
(3 answers)
Closed 3 years ago.
I have Two fields like time-from and time-to and time-took for particular people so my job is
ex: time-from: 10:00:00
time-to : 1:20:10
time-took : 30 (min)
So i want to display the order like that:
10:00:00 , 10:30:00 11:00:00, 11:30:00, 12:00:00, 12:30:00, 1:00:00, 1:20:10
If you convert the times into a time stamp using the PHP function "mktime", you then have the number of seconds difference between the dates. If the date is irreverent, then you could just use the same day for all of the times.
// The time to get the number of seconds from.
$tmpStr = "01:10:20";
// Break the string into components.
$timeArray = split(":", $tmpStr);
// The date element is redundant but needs to be set.
$timeInSeconds = mktime ($timeArray[0], $timeArray[1], $timeArray[2], 1, 1, 1970);
Now having the time in seconds from midnight allows you to order them accordingly and to also compare two times to find out the difference.

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

Extended hours in php [duplicate]

This question already has answers here:
Calculate time elapsed in php - spans over 24 hours
(2 answers)
Closed 9 years ago.
I have been searching for a while now regarding on how to format hours in php that will show 000:00:00 instead of 00:00:00.
I need this to calculate the duration of records with start and end Date with a datetime data type in mysql.
The problem with my current format ('H:m:s') is it only count 24 hours and when the duration goes higher than that, it will show 01:00:00 instead of 25:00:00.
Try TIMEDIFF() function in MySQL
Example:
SELECT TIMEDIFF('2008-12-31 23:59:59.000001',
'2008-12-30 01:01:01.000002');
-> '46:58:57.999999'

Categories