Calculate date interval in PHP [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Subtracting dates in PHP
I have two Unix timestamps, how can I calculate the total number of days between them?

$timestamp1 = x;
$timestamp2 = y;
$days_elapsed = floor(($timestamp2 - $timestamp1)/86400);
echo $days_elapsed;

Convert them to UNIX-timestamp (if they arent already), then just
$diff = abs($timestamp1 - $timestamp2);
$days = (int) ($diff / 60 / 60 / 24);

something like this should do the trick:
$days = (strtotime($timestamp1)-strtotime($timestamp2))/(60*60*24);

This code should do the trick
$numDays = abs($timeOne - $timeTwo)/60/60/24;

Related

Datediff round returns zero [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 months ago.
I'm trying to run a datediff. I essentially want to say the number of days between 1st November 2022 and 1st November 2022 is ONE day.
When I attempt this, $number_of_days returned is 0. Can someone explain why this is and how to resolve?
$end = "2022-11-01";
$start = "2022-11-01";
$datediff = ($end - $start) + 1; // tried to fix
echo $datediff;
echo '<hr>';
echo ($datediff / (60 * 60 * 24));
$number_of_days = round($datediff / (60 * 60 * 24));
echo '<hr>';
echo $number_of_days;
The dates you shown in the code are strings, they are not numerically "minorable". You need to convert them into a int of time first like this:
$end = strtotime("2022-11-01");
$start = strtotime("2022-11-01");
$datediff = (($end - $start)/60/60/24) + 1;
Why doesn't it work: If you try to subtract strings like this, PHP will auto-convert the strings into the boolean value true and then convert them into the integer 1. You divided it by (60 * 60 * 24) which results in a very small number 1.1574074074074E-5 which then be rounded into 0.
Your subtraction is using string values, not date values, so actually ends up as being 2022 - 2022, which equals zero...
(newer versions of PHP will complain "Notice: A non well formed numeric value encountered")
You could convert to instances of DateTime, and then determine the difference that way:
$end = "2022-11-01";
$start = "2022-11-01";
// convert to DateTime
$dtEnd = DateTime::createFromFormat('!Y-m-d', $end);
$dtStart = DateTime::createFromFormat('!Y-m-d', $start);
// get the difference in days
$days = $dtEnd->diff($dtStart)->format('%a') + 1;

How to sum up time in php? [duplicate]

This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;

How to calculate interval in 30 minutes between two dates? [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have for example:
$first = '2014-05-18 12:00:00';
$last = '2014-05-18 13:30:00';
and i would like calculate interval in 30 minutes for these dates.
For this example interval = 3.
Other example:
$first = '2014-05-18 11:00:00';
$last = '2014-05-18 14:00:00';
interval = 6;
How is the best way for this in PHP?
Simple math solution
$s=floor(abs(strtotime($first)-strtotime($last))/30/60);
Here is fiddle
https://eval.in/153120
function timeDiff30minutes($first,$last){
$first = new Datetime($first);
$last = new Datetime($last);
$time_diff = $first->diff($last);
$time_diff_minute = $time_diff->days * 24 * 60;
$time_diff_minute += $time_diff->h * 60;
$time_diff_minute += $time_diff->i;
return $time_diff_minute/30;
}
timeDiff30minutes('2014-05-18 11:00:00','2014-05-18 14:00:00') // output 6

Php calculete the cost for each day passed [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP calculate days Between 2 different dates [duplicate]
(2 answers)
Closed 9 years ago.
$1date =$row['Date1'];
$2date = $row['Date2'];
$datediff = $1date - $2date;
echo $datediff;
I want to count the days between and put into a table the result(10 dollars for each day passed)
// convert to unix timestamp
$1date = strtotime($row['Date1']);
$2date = strtotime($row['Date2']);
// 86400 seconds in a day
// floor to round down, change to ceil to round up
$datediff = floor(($1date - $2date) / 86400);
$cost = $days * 10;
You could do it in MySQL then work it into a variable. Do the initial call
SELECT TIMESTAMPDIFF('Date1','Date2');
Try:
$days = date_diff(date_create($row['Date1']), date_create($row['Date2']))->format('%a');
$cost = $days * 10;

Difference of hours between two dates (integer) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate number of hours between 2 dates in PHP
How can I get the number of hours of difference between two dates in PHP? I need to get an integer since I want to know if is bigger or smaller than a particular value.
Try:
$date1 = "2012-11-05 12:35:00";
$date2 = "2012-11-07 14:35:00";
$diff = strtotime($date2) - strtotime($date1);
$diff_in_hrs = $diff/3600;
print_r($diff_in_hrs);
Manual
Demo
If you have an up-to-date PHP
$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');
// Procedurally
$interval = date_diff($dateOne, $dateTwo);
// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);
See: http://www.php.net/manual/en/class.dateinterval.php
<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>

Categories