0

I want to calculate the difference between today and next year April 4 (right now is 4/4/2013), but I don't know how to create a DateTime object using actual year + 1. This is what I have:

$now = new DateTime();
$ref = mktime(0, 0, 0, 4, 3, date("Y")+1);
echo $diff = $ref->diff($now)->days;

I think the problem is that mktime is not returning a DateTime object? Which is the best way to do this? Thanks

Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85

3 Answers3

1
$today = date("Y-m-d");
$destination = date("Y-m-d" , mktime(0, 0, 0, 4, 3, date("Y")+1));

$todayObj = new DateTime($today);
$destObj = new DateTime($destination);

echo $diff = $todayObj->diff($destObj)->days;

Didn't test it yet.

Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
1

Use strtotime for this as below

$nextyear = date('Y')+1;
$time1=$nextyear.'-'.date('m').'-'.date('d');

$time2=date('Y-m-d');

echo $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
GBD
  • 15,847
  • 2
  • 46
  • 50
  • 1
    it will not work when the current year will be 2013. In that case I will need to know the difference between the current day (in 2013) and 4-3-2014 – Tomas Ramirez Sarduy Oct 12 '12 at 08:58
  • 1
    @Tom it was example how to find hours between two dates. anyway see updated answer now – GBD Oct 12 '12 at 09:05
  • This is what i was looking for: `$time1=date('Y')+1.'-'.date('m').'-'.date('d');` I already know how to subtract days between two dates ;) – Tomas Ramirez Sarduy Oct 12 '12 at 09:08
0

Try this:

print_r(date_diff(date_create(), date_create('2013-04-03 00:00:00')));

Outputs:

DateInterval Object
(
    [y] => 0
    [m] => 5
    [d] => 22
    [h] => 9
    [i] => 8
    [s] => 25
    [invert] => 0
    [days] => 173
)
Usman Salihin
  • 291
  • 1
  • 8