views:

37

answers:

4

I want to get the absolute days away a datetime is from today. For example, i would like to know if a date is 2 days away, or 78 days away, or even 5,239 days away (not likely, but you get the idea). I am using an MS SQL database which is returning datetimes where the time components are all 00:00:00.

date_diff returns relative values that you then have to do some crazy math with to get absolute dates do to calculating months, years, etc.

Also, i am having issues getting the date component only of today's date in php.

Edit: Thanks to mr. w. This is what i ended up with:

    $date = $row['AirdateDateTime'];
    $today = date_create(date("Y-m-d"));
    $away = date_diff($today, $date);
    $d = $away->format("%R%a");

The date_create() part was the part i was originally missing to convert to an actual datetime. Also, the format needs to be %R%a. Using %R%d only works for dates in this month.

+1  A: 

It sounds like what you want is

<?php

$yourDate = '2010-10-05';
echo ciel((strtotime($yourDate) - time()) / 60 / 60 / 24);
alex
You see, this is similar to the problems i was having. This returns "3", but that date is only 2 days away. Or more precisely -2 days away.
Try removing `round()`.
alex
True. This function will be correct if you replace the round() with floor() and multiply the result by -1, OR replace the order of the arguments and replace round() with ceil().
+1  A: 

It's probably easiest to convert to unix timestamps (seconds since 1970) and then get the difference.

strtotime() and time() are your friends.

UltimateBrent
The only issue here is timezones and DST, but sensible databases only contain UTC datetimes anyway (and possibly a timezone offset in a separate column).
You
+1  A: 

An alternative to what has already been suggested is the DateTime library:

$today = date("Y-m-d");
$today = new DateTime($today);
$future = new DateTime('2010-10-25');
$interval = $today->diff($future);
echo $interval->format('%d days away'); //outputs 17 days away
Russell Dias
does this work for something 45 days away, or would it just print 15?
It's going to return whatever the difference is - it's calculated at runtime. The %d gets replaced with the difference as measured in days; for other units, use other tokens. The docs I referenced in my answer give the details on this.
mr. w
Why would it just print `15` ?
Russell Dias
I was thinking it would return 15 because $interval->d returns 15, as in 15 days plus 1 month.
Not to say I told you so, but I was right. Change that date to `'2010-11-25'` and your result will be 18 days. The format needs to be `%a`
Oh I see what you were trying to say earlier regarding the `15`. Yes, my answer will only specify days within the respective month. So, your options are either specifying a month `%m` or as you have already figured out the `%a` format.
Russell Dias
+1  A: 

The date_diff() function (really, the DateTime::diff() method) is what you want, and it's actually not hard. In fact, I think the example in the docs is exactly what you're after:

<?php
    $datetime1 = new DateTime('2009-10-11');
    $datetime2 = new DateTime('2009-10-13');
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%d days');
?>

or

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

What's returned is a DateInterval object, which you can format any way you want with its format() method. Above, it's being formatted to days, but you have a ton of options; see http://us.php.net/manual/en/dateinterval.format.php.

You shouldn't need to do any math yourself.

[EDIT - forgot this part]

As for getting the date component of today's date, you can do something like this:

<?php
    echo date('Y-m-d');
?>

See http://us.php.net/manual/en/function.date.php.

mr. w
I was using this method before, but hadn't tried out the format method. My goal is to get a signed integer out of it. I assume I can slightly modify this code and get it to work
The %R token puts a '+' or '-' in front of the days value, so the output might be something like "+3 days" or "-2 days". If you only want the sign when it's negative, use a lowercase 'r' (i.e., "%r%d days"). If you don't want the label part, just stick to "%R%d" or "%r%d".
mr. w
Thats what i figured. This worked, thanks. The full code i used is in my post if anyone is interested. As far as the 2nd part of your post, i had already tried it, but it doesnt work, you need date_create() in front of it. I also tried getdate() but that wasn't working either.
I just realized this is still incorrect due to a slight formatting bug: the format needs to be `%R%a`. Using `%R%d` only works for dates in this month.