Reputation: 21
I have a schoolproject where I have to make an agenda. I'm having problems with my calculation function. So the function below needs to calculate the remaining days untill my next appointment. I have a future date($timefromdb) stored in my mysql database and I want to calculate the difference between the current date and the date from my database.
public function getAllAfspraken(){
$db = new Db();
$sql = "SELECT * FROM `tblafspraken`";
$return = $db->conn->query($sql);
while($row = mysqli_fetch_array($return))
{
$currentdate= date("Y-m-d");
$timefromdb = $row['af_datum'];
$timeleft = $currentdate-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo "<h3>".$row['af_datum']."</h3>";
echo "<p>" .$row['af_beschrijving']. "</p>";
echo $daysleft;
echo "<hr />";
}
}
Upvotes: 0
Views: 2505
Reputation: 6252
You could take this a step further, and calculate years, months, days, hours, etc... For example if you're using the MySQL date format, a function like this may work:
function get_time_diff($start_date, $end_date = null) {
// make sure start date is in mysql datetime format
$expr = "/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}?$/";
if (!preg_match($expr, $start_date)) {
return false;
}
if (isset($end_date)) {
// make sure end date is in mysql datetime format
if(!preg_match($expr, $end_date)) {
return false;
}
list($date, $time) = explode(' ', $end_date);
list($hour, $minute, $second) = explode(':', $time);
$today = new DateTime($date);
$today->setTime($hour, $minute, $second);
} else {
$today = new DateTime();
}
$target = new DateTime($start_date);
$diff = $today->diff($target);
if ($diff->y > 0) {
echo $diff->y . ' year' . ($diff->y > 1 ? 's' : '') . ($diff->m > 0 ? ', ' : '');
}
if ($diff->m > 0) {
echo $diff->m . ' month' . ($diff->m > 1 ? 's' : '') . ($diff->d > 0 ? ', ' : '');
}
if ($diff->d > 0) {
echo $diff->d . ' day' . ($diff->d > 1 ? 's' : '') . ($diff->h > 0 ? ', ' : '');
}
if ($diff->h > 0) {
echo $diff->h . ' hour' . ($diff->h > 1 ? 's' : '') . ($diff->i > 0 ? ', ' : '');
}
if ($diff->i > 0) {
echo $diff->i . ' minute' . ($diff->i > 1 ? 's' : '') . ($diff->s > 0 ? ', ' : '');
}
if ($diff->s > 0) {
echo $diff->s . ' second' . ($diff->s > 1 ? 's' : '');
}
return $today < $target ? ' until' : ' ago';
}
if ($test = get_time_diff('2012-09-11 10:25:00')) {
echo $test;
} else {
echo 'invalid request';
}
This would output something like: 1 year, 7 months, 14 days, 5 hours, 58 minutes, 6 seconds ago
You really shouldn't use strtotime() or unix timestamps due to their limited date range.
Upvotes: 0
Reputation: 270
Try using MySQL:
$sql = "SELECT *, DATEDIFF(NOW(), af_datum) AS timeleft FROM `tblafspraken`";
Also, your code looks like it is finding days since, not days remaining. If the results are not correct, try using DATEDIFF( af_datum, NOW()) instead.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
Upvotes: 1
Reputation: 2350
I would try using the strtotime function to convert both values like this:
$currentdate= strtotime( date("Y-m-d") );
$timefromdb = strtotime( $row['af_datum'] );
$daysleft = ($timefromdb - $currentdate)/(60 * 60 * 24);
http://www.php.net/manual/en/function.strtotime.php
Upvotes: 1
Reputation: 44874
Use DateTime:diff
for the calculation
$date1 = new DateTime("2014-04-30");
$now = new DateTime();
$diff = $date1->diff($now);
echo 'Remaining Days ::'. $diff->days ;
The above will show +ve or -ve
You can also use $diff->format('%R%a days');
for +ve or -ve
//print_r($diff);
Complete data for $diff
is as below and can find difference on different parameters like days, hour , minute etc.
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 4
[h] => 10
[i] => 3
[s] => 46
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 4
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
Upvotes: 3