我需要比較今天的日期。基本上我把它放在2/17/13,它會輸出「昨天」。我試過echo date('l jS F', strtotime('2/15/13'));
,但它只會顯示該日期,不會與今天進行比較。計算日期/時間PHP
-1
A
回答
0
function relativeTime($time) {
$d[0] = array(1,"second");
$d[1] = array(60,"minute");
$d[2] = array(3600,"hour");
$d[3] = array(86400,"day");
$d[4] = array(604800,"week");
$d[5] = array(2592000,"month");
$d[6] = array(31104000,"year");
$w = array();
$return = "";
$now = time();
$diff = ($now-$time);
$secondsLeft = $diff;
for($i=6;$i>-1;$i--)
{
$w[$i] = intval($secondsLeft/$d[$i][0]);
$secondsLeft -= ($w[$i]*$d[$i][0]);
if($w[$i]!=0)
{
$return.= abs($w[$i]) . " " . $d[$i][1] . (($w[$i]>1)?'s':'') ." ";
}
}
$return .= ($diff>0)?"ago":"left";
return $return;
}
0
從http://www.highlystructured.com/comparing_dates_php.html
$exp_date = "2006-01-16";
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) {
$valid = "yes";
} else {
$valid = "no";
}
0
下面是一個例子,讓你開始:
$when = '2/17/13';
$now = new DateTime();
// I append $now's time to $then, to make sure we compare
// the two dates using the same time of the day
// if you want to compare to $then at 00:00:00, leave it out
$then = new DateTime($when . ' ' . $now->format('H:i:s'));
$diff = $now->diff($then);
switch($diff->days)
{
case 0:
echo 'today';
break;
case 1:
echo $diff->invert ? 'yesterday' : 'tomorrow';
break;
default:
echo $diff->invert ? $diff->days . ' days ago' : $diff->days . ' days from now';
break;
}
0
的Drupal附帶了一個非常漂亮的format_interval($interval, $granularity = 2, $langcode = NULL)
function。
<?php
function format_interval($interval, $granularity = 2, $langcode = NULL) {
$units = array(
'1 year|@count years' => 31536000,
'1 month|@count months' => 2592000,
'1 week|@count weeks' => 604800,
'1 day|@count days' => 86400,
'1 hour|@count hours' => 3600,
'1 min|@count min' => 60,
'1 sec|@count sec' => 1,
);
$output = '';
foreach ($units as $key => $value) {
$key = explode('|', $key);
if ($interval >= $value) {
$output .= ($output ? ' ' : '') . format_plural(floor($interval/$value), $key[0], $key[1], array(), array('langcode' => $langcode));
$interval %= $value;
$granularity--;
}
if ($granularity == 0) {
break;
}
}
return $output ? $output : t('0 sec', array(), array('langcode' => $langcode));
}
?>
你不需要運行Drupal來使用它。只需在某處添加上述功能即可。不過,上面的函數還會調用format_plural()
--它調用t()
(都是自定義的Drupal函數),因此您需要修改上述函數或包含所有這些函數。
你的使用情況:
<?php
$today = date();
$compare = strtotime('2/15/13');
$interval = ($today - $compare);
print format_interval($interval, 1);
// Outputs '1 day'
?>
0
function checkDate($todaysDate,$expirationDate){
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) {
$valid = "yes";
} else {
$valid = "no";
}
return $valid
}
$expirationDate = "2006-01-16";
$todaysDate = date("Y-m-d");
$valid=checkDate($todaysDate,$expirationDate)
做一個函數,那麼你可以用日期的組合使用。然後你可以用一些條件來擴展函數來確定它的「昨天」。
相關問題
- 1. PHP/MySQL:日期時間計算?
- 2. MySQL日期/時間計算
- 3. 時間/日期計算
- 4. 日期時間計算C#
- 5. dos日期/時間計算
- 6. php:日期時間算術
- 7. PHP日期計算
- 8. 計算日期之間的時間差
- 9. 何時涉及日期/時間計算?
- 10. PHP計算兩個日期之間的辦公時間數
- 11. 用PHP計算日期/時間之間的差異
- 12. 日期時間時區mysql和php的計算
- 13. C#日期時間計算和ToString
- 14. jquery日期和時間計算Firefox NaN
- 15. Python Ephem /日期時間計算
- 16. 日期時間timedelta內計算?
- 17. 計算兩個日期時間
- 18. 學說:計算日期時間差
- 19. 計算過去在C#日期時間
- 20. Sharepoint日期時間計算列
- 21. 計算起始日期和時間
- 22. 乳膠日期日期時間計算月份日年
- 23. 計算日期時間差只獲取時間期限
- 24. 如何使用PHP計算兩個日期之間的日期?
- 25. php日期計算循環?
- 26. 使用php計算日期
- 27. 日期間隔分配計算 - 日期計算
- 28. 我需要計算一個日期時間列之間的日期/時間差
- 29. 計算比較sql時間戳和php日期(「y-m-d h:i:s」)
- 30. PHP計算時間
[計算相對時間]的可能重複(http://stackoverflow.com/questions/11/calculating-relative-time) – jeremyharris 2013-02-18 16:05:18