1

I have 2 unix time, one is todays date and other is expiration time.

  • Todays Time: 1377173245 (2013-08-22 12:07:25)
  • Expiration Time: 1406303166 (2014-07-25 15:46:06)

What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?

Thank you :-)

Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
prototype
  • 59
  • 1
  • 3
  • 12

5 Answers5

4

This is what you're looking for: DateTime::diff

First create object with 1st date:

$date = new DateTime('2013-08-22 12:07:25');

Then use the diff method:

$diff = $date->diff(new DateTime('2014-07-25 15:46:06'));

Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.

Syscall
  • 19,327
  • 10
  • 37
  • 52
  • Hello Michal, it show the following error, Call to undefined method DateTime::diff() – prototype Aug 22 '13 at 12:33
  • Oh.. I see it require php 5.3 or higher – prototype Aug 22 '13 at 12:35
  • Ya, its working. thanks a lot friend I appreciate. Also, thank you everyone who suggested the answers. – prototype Aug 22 '13 at 12:41
  • If all you need to check is whether the expiration date-time has been crossed, simply check the "invert" index from the $diff array. If its 1. then the returned interval is negative and hence the expiration limit has been crossed. – jahackbeth May 17 '15 at 20:03
2

Use DateTime::diff

See the doc on https://www.php.net/manual/en/datetime.diff.php

Syscall
  • 19,327
  • 10
  • 37
  • 52
netvision73
  • 4,831
  • 1
  • 24
  • 30
1

Try using datetime::diff. Here's the example from the php.net documentation:

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.

Syscall
  • 19,327
  • 10
  • 37
  • 52
0

Use DateTime class:

Example:

$datetime1 = new DateTime();
$datetime2 = new DateTime('2014-07-25 15:46:06');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');

Outputs:

11 months, 21 hours, 44 minutes, and 20 seconds remaining 
Syscall
  • 19,327
  • 10
  • 37
  • 52
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
-1

It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.

$days=($expiration-$today)/(60*60*24);
$hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder
$minutes=(($expiration-$today)/(60))%(60*60);
$seconds=($expiration-$today)/60;

If you need months, you'll find it easier to use date_diff().

IanPudney
  • 5,941
  • 1
  • 24
  • 39