0

I'm trying to calculate the following value, which I'm getting from my database 2014-08-29 04:37:36 I want to calculate this value within current date time.

Like my above value is 2014-08-29 04:37:36 but I want to show 3 days left instead 2014-08-29 04:37:36. If my value is 2014-08-23 04:37:36 it should show -2 days left.

Whatever my value is, i want to calculate it and show the time left, if it expired than it should show a minus sign as I explained above. Can anyone help me please!!

This question is much simpler and different than the references given to mark as duplicate. My question is clear and simple, not complex to the references given by HAL9000, Boann, karthikr, amphetamachine, Shankar Damodaran. Also the references are misleading my question.

Sahed
  • 65
  • 1
  • 7

3 Answers3

0

Use the PHP DateTime::diff function described here

Quick example:

<?php

$date1 = new DateTime("now");
$date2 = date_create('2014-12-10');

$diff = date_diff($date1, $date2);

echo $diff->format('%R%a days');

demo

The nice thing about this code is that you can change the return string format easily using DateInterval::format

RichardBernards
  • 3,146
  • 1
  • 22
  • 30
  • Actually; PHP < 5.3, but I don't think answers should be discarded because they don't work with older versions of PHP, especially when that older release has been released 5 years ago. – RichardBernards Aug 25 '14 at 10:13
  • Of course not ;) just wanted to point it out, since I had the same issue a few weeks ago on a project running on PHP < 5.2 I guess – hex494D49 Aug 25 '14 at 10:15
0

Try the snippet below

<?php
$datestr = "2014-08-28 19:10:18";
$date = strtotime($datestr);

$diff = $date - time();         
$days = floor($diff / (60 * 60 * 24));  
$hours = round(($diff - $days * 60 * 60 * 24) / (60 * 60));

echo "$days days $hours hours left";
?>

Output:

3 days 9 hours left

Demo

hex494D49
  • 9,109
  • 3
  • 38
  • 47
0

Try This

$dateNow = new DateTime(date('Y-m-d H:i:s'));
$getDate = new DateTime("2014-08-29 04:37:36");
$diff= $getDate->diff($dateNow);
$return = $diff->days;
if($dateNow>$getDate)
  echo "-".$return." Days Left";
else
  echo $return." Days Left";
Habibul Morsalin
  • 180
  • 1
  • 11