Viewed   74 times

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?

 Answers

5
<?php 
$cdate = mktime(0, 0, 0, 12, 31, 2009, 0);
$today = time();
$difference = $cdate - $today;
if ($difference < 0) { $difference = 0; }
echo "There are ". floor($difference/60/60/24)." days remaining";
?>
Thursday, August 18, 2022
3

DateInterval is buggy on windows platform. See bug #51183. The official answer seems to be "use VC9 builds instead for now".

Monday, August 15, 2022
 
3

I use following wrapper class in my php5.2 apps: http://pastebin.ca/2051944. Untill php5.3 was released - it saves much my time

Thursday, November 3, 2022
 
3

PHP fragment:

<?php

//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)

//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));

//Report
echo "$days days $hours hours remain<br />";
?>

Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.

Sunday, November 27, 2022
 
1

The problem is that you're basing the month on April being 4, when April is 3 in Javascript. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#Parameters

var diffDays1=(function(){ 
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    var secondDate = new Date(new Date().getFullYear()+1,3,5);
    var firstDate = new Date();
    return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
})();
Saturday, November 5, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :