鉴于某个日期,最简单的方法是确定该日期前的天数(以PHP为单位)?我正在尝试构建倒计时小部件,谢谢!
扩展schnaader的答案,这里是一个单行函数,它将日期字符串作为参数但只返回天数:
不要将日期视为整数.使用您的数据库,它有很好的支持处理日历/时间.
select datediff("2009-11-12", now())
天分钟和秒格式:
// 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;
PHP 5.3引入了实现'diff'功能的DateTime类.见http://www.php.net/manual/en/datetime.diff.php