32

I am trying to build a count-down widget.

Given a certain date, whats the easiest way in PHP to determine how many days until that date?

ASh
  • 34,632
  • 9
  • 60
  • 82
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

6 Answers6

56
       <?php
         $cdate = mktime(0, 0, 0, 12, 31, 2009);
         $today = time();
         $difference = $cdate - $today;
         if ($difference < 0) { $difference = 0; }
         echo floor($difference/60/60/24)." days remaining";
       ?>
PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
schnaader
  • 49,103
  • 10
  • 104
  • 136
  • 2
    This really depends on your definition "days until". For example this snippet tells me new years eve is 34 days away, and wolfram alpha, google, siri, and just about every other site i search is telling me 35 days. FWIW – AlBeebe Nov 27 '12 at 00:26
  • @AlBeebe then just add +1. What's so hard about that? – Devfly Mar 16 '13 at 13:53
  • I think that's because the snippet counts the days till Dec 31 00:00, not till what most people think of new years eve (Dec 31 23:59 or Jan 01 00:00) – schnaader Mar 16 '13 at 14:46
  • Please make note that in mktime(): The is_dst parameter is now [deprecated](http://php.net/manual/en/function.mktime.php). – Sandeep Singh Jun 28 '15 at 18:02
  • Where's the love for `DateTime`? I know this is an old answer but using `mktime` feels a bit archaic at this point when `DateTime` is much more reliable these days than any existing PHP date function. – Wes Dec 16 '16 at 02:40
31

Expanding on schnaader's answer, here is a one-liner function that takes a date string as a parameter but only returns the number of days:

<?php
function days_until($date){
    return (isset($date)) ? floor((strtotime($date) - time())/60/60/24) : FALSE;
}
?>
Jeff Hines
  • 3,511
  • 1
  • 19
  • 21
9

Don't treat dates as integers. Use your database, which has good support for dealing with calendars/time.

select datediff("2009-11-12", now())
troelskn
  • 115,121
  • 27
  • 131
  • 155
9

PHP 5.3 has introduced the DateTime class that implements a 'diff' function. See http://www.php.net/manual/en/datetime.diff.php

schuilr
  • 674
  • 1
  • 7
  • 11
8

Days minutes and seconds format:

// current time
$today = new DateTime(format_date(time(), 'custom', 'd M Y H:i:s'));
// date to which we want to compare (A Drupal field in my case).
$appt  = new DateTime(format_date($yourdate_is_timestamp, 'custom', 'd M Y H:i:s' ));

// Months
$months_until_appt = $appt->diff($today)-> m;
// days
$days_until_appt = $appt->diff($today)-> days;
// hours
$hours_until_appt = $appt->diff($today)-> h;
// minutes
$minutes_until_appt = $appt->diff($today)-> i;
// seconds
$seconds_until_appt = $appt->diff($today)-> s;

echo 'days until: ' . $days_until_appt;
echo 'hours until: ' . $hours_until_appt;
echo 'minutes until: ' . $minutes_until_appt;
echo 'seconds until: ' . $seconds_until_appt;
Alejandro Moreno
  • 5,578
  • 2
  • 31
  • 29
  • `DateTime` class FTW – Oliver Tappin May 01 '16 at 15:01
  • 1
    According to http://php.net/manual/en/ref.datetime.php `->days` (for example) is not a valid class method. Can you please modify your code to use `->format('d')`, `->format('m')`, etc. so it's more clear for those visiting? – Wes Dec 16 '16 at 02:38
  • For the record - I updated for use of `DateTime` - can't believe the accepted answer using used `DateTime` in the solution. – Wes Dec 16 '16 at 02:39
2

I have just come across this in my code for a live app where the system incorrectly regarded today and tomorrow as today. We have just gone into British Summer Time and this has caused a problem with our app.

I am now using the following, which is giving me the correct result:

function days_away_to($dt) {
    $mkt_diff   = strtotime($dt) - time();
    return floor( $mkt_diff/60/60/24 ) + 1; # 0 = today, -1 = yesterday, 1 = tomorrow
}

Of course, using the DateTime class is the best solution going forward ...

  • 1
    This is exactly the same code that [was already posted here](http://stackoverflow.com/a/8807610/447356) and you just changed the variable names. -1 for trying to get rep for other people's work and wasting our time. – Shadow The GPT Wizard Mar 30 '14 at 11:59
  • Thanks SW but unfortunately that solution did not work for me: it is giving me 0 for 2014-03-31 telling me that that is the date for today (it is 30 March now in the UK). I have added +1, which corrects it for me. – Bashir Patel Mar 30 '14 at 14:06
  • So you should highlight this in your post by linking to that other answer and explaining that you just added 1 to make it work. Please make the edit and let me know. – Shadow The GPT Wizard Mar 30 '14 at 14:17