1

I have a date and time saved in a database, in the following format: 2016-04-03 12:54:11

Basically, this date and timestamp represents the exact date and time something was created. What I'm trying to do is display a second date and time, that is the exact number of days since the first timestamp.

So if the timestamp in the dataase was 2016-04-03 12:54:11 and todays date and time is 2016-04-04 12:54:11, it would display: Overdue by: 1 Day

So far I have:

<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
                $dateNow = time();

                $dateDifference = abs(strtotime($dateCreated) - strtotime($dateNow));

                $years = floor($dateDifference / (365*60*60*24));
                $months = floor(($dateDifference - $years * 365*60*60*24) / (30*60*60*24));
                $days = floor(($dateDifference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); ?>

And then:

echo "<strong>Overdue by:</strong> $days Days.";

however, this code displays that 2 dates, only a day apart, as 14 days apart.

Help would be appreciated, cheers.

J. Doe
  • 47
  • 4

4 Answers4

1

Try using DateTime objects. They'll make your life much easier. The above could then be achieved with the following:

$date1 = new DateTime('2016-04-03 12:54:11');
$date2 = new DateTime('2016-04-04 12:54:11');

$diff = $date1->diff($date2);

The $diff variable will then be a DateInterval object, which has a days property to show the amount of days between the two dates. In this case 1, so you can do:

echo "Overdue by: " . $diff->days . " days.";

Which will output Overdue by: 1 days..

On a side-note: You should really not be using mysql_ functions anymore. They have been deprecated for years and are no longer part of the PHP core in the latest PHP version. So this code will not work on an up-to-date server. Also see: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • I'm aware of the mysql_ functions, I'm just quickly creating something for a demonstration :) Thanks though! – J. Doe Apr 04 '16 at 12:44
0

I think the best way to do it is using the Php DateTime class witch allow us to do it.

Try this:

$datetime1 = new DateTime("YOUR_DB_TIMESTAMP");
$datetime2 = new DateTime("today");

$difference = $datetime1->diff($datetime2);

echo difference->d;

Also, you can choose the way that you want to show the diference.

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

Reference:

Example-> Finding the number of days between two dates
DateTime Doc -> http://php.net/manual/es/class.datetime.php Regards!

Community
  • 1
  • 1
Idir Ouhab Meskine
  • 587
  • 1
  • 9
  • 23
0

I would use the DateTime object for things like this, because are really a very big help.

$dateCreatedObj = DateTime::createFromFormat('Y-m-d H:i:s', $dateCreated);
$dateNowObj = new DateTime();
$dateDifference = $dateCreatedObj->diff($dateNowObj);
echo $dateDifference->format('Overdue by: %a Day');

Please note that this does not handle timezones, multilingual and plural issues.

ST2OD
  • 705
  • 1
  • 5
  • 15
0

Diff function returns the difference between two DateTimeInterface objects.

You can try to use DateTime object :

<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
         $dateNow = new DateTime();
         $dateOther = new DateTime($dateCreated);
         $interval = $dateNow->diff($dateOther);
         echo $interval->format('%R%a days');
DevLoots
  • 747
  • 1
  • 6
  • 21