iT邦幫忙

0

PHP對日期的問題

php

下方是得到某日期一年後的日期,我想瞭解是否有其他的方式可以更簡潔的得到答案?

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>date</title>



<?php
$date = '2010-09-22 13:22:32';
$y = date('Y', strtotime($date)); // 年
$m = date('m', strtotime($date)); // 月
$d = date('d', strtotime($date)); // 日
$h = date('H', strtotime($date)); // 時
$i = date('i', strtotime($date)); // 分
$s = date('s', strtotime($date)); // 秒
$newDate = date('Y-m-d H:i:s', mktime($h, $i, $s, $m, $d, $y + 1)); // 一年後的時間

echo '起始日期:' . $date . '<br />';
echo '一年後為:' . $newDate;
?>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

16
fillano
iT邦超人 1 級 ‧ 2010-01-20 13:55:47
最佳解答

這樣嗎?

<pre class="c" name="code">
<?php
$t = strtotime('2010-09-22 13:22:32');
if(閏年){
  $t += 60*60*24*366;
}else{
  $t += 60*60*24*365;
}
echo date('Y-m-d H:i:s',$t);
?>

另外,建議您多上http://tw2.php.net/manual/en/找找可以使用的函數,這個問題應該不難解決的。

chan15 iT邦新手 2 級 ‧ 2010-01-20 16:15:02 檢舉

剛由另一個討論區找出最簡潔的寫法了 XD

<pre class="c" name="code">
$date = '2010-09-22 13:22:32';
$newDate = date('Y-m-d H:i:s', strtotime("+1 years",strtotime($date)));
fillano iT邦超人 1 級 ‧ 2010-01-20 23:29:36 檢舉

喔喔喔,有趣。不過看起來其實可以縮成一行。

我要發表回答

立即登入回答