dongyan2469 2013-08-26 05:56
浏览 16
已采纳

PHP获取特定日期最近发生的年份

I am working on a script that will determine when a specific calendar date most recently occurred.

This post, Get the year of a specified month in previous 12 months?, is the basis of my current beta. It was written for a monthly result and not a specific date.

<?php
if (empty($_GET['m'])) {
    $m = "October";
} else {
    $m = htmlspecialchars($_GET['m']);
}

if (empty($_GET['d'])) {
    $d = "1";
} else {
    $d = htmlspecialchars($_GET['d']);
}

date_default_timezone_set('America/Los_Angeles');

$m = date('m', strtotime($m));

$c = (mktime(0,0,0,$m,$d) < time())
    ? mktime(0,0,0,$m,$d)
    : mktime(0,0,0,$m,$d,date("Y") -1);

$from = date('F jS', $c) . " last occurred in " . date('Y', $c) . ".";

echo $from;
?>

The result of the default value, October 1 is: "October 1st last occurred in 2012."

For June 11th, as a random example: "June 11th last occurred in 2013."

All date results are TRUE, except for the result for February 29th: "March 1st last occurred in 2013." Obviously, this is a false statement since the desired result is: "February 29th last occurred in 2012."

I'm kind of stuck, what is the best way to add a true leap year result?

  • 写回答

2条回答 默认 最新

  • duanfen1992 2013-08-26 06:17
    关注

    This is calculating last true year. Replace with following codes.

    <?php
    if (empty($_GET['m'])) {
        $m = "October";
        } else {
        $m = htmlspecialchars($_GET['m']);
    }
    
    if (empty($_GET['d'])) {
        $d = "1";
        } else {
        $d = htmlspecialchars($_GET['d']);
    }
    
    date_default_timezone_set('America/Los_Angeles');
    
    $y = date('Y');
    if($m == "February" && $d == "29") $y = $y - ($y % 4);
    $m = date('m', strtotime($m));
    
    $c = (mktime(0,0,0,$m,$d) < time()) ? mktime(0,0,0,$m,$d,$y) : mktime(0,0,0,$m,$d,$y);
    
    $from = date('F jS', $c) . " last occurred in " . $y . ".";
    
    echo $from;
    ?>
    

    Results

    For 2013: Result 2012
    For 2012: Result 2012
    For 2011: Result 2008
    For 2010: Result 2008
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示