开发者社区> 问答> 正文

php如何获取每年中每月的起始日期?

比如2015年里面 1月份的其实日期是2015-01-01 2015-01-31我想把每年里面每个月的起始日期分别放在一个数组里面 请问这个怎么解决?

展开
收起
小旋风柴进 2016-03-09 15:38:07 2631 0
1 条回答
写回答
取消 提交回答
  • ?php
    header("Content-type:text/html;charset=utf-8");
    date_default_timezone_set("Asia/Shanghai");
    $year = (int)$_GET['year'];
    $week = (int)$_GET['week'];
    $weeks = date("W", mktime(0, 0, 0, 12, 28, $year));
    echo $year . '年一共有' . $weeks . '周<br />';
    if ($week > $weeks || $week <= 0)
    {
     $week = 1;
    }
    if ($week < 10)
    {
     $week = '0' . $week;
    }
    $timestamp['start'] = strtotime($year . 'W' . $week);
    $timestamp['end'] = strtotime('+1 week -1 day', $timestamp['start']);
    echo $year . '年第' . $week . '周开始时间戳:' . $timestamp['start'] . '<br />';
    echo $year . '年第' . $week . '周结束时间戳:' . $timestamp['end'] . '<br />';
    echo $year . '年第' . $week . '周开始日期:' . date("Y-m-d", $timestamp['start']) . '<br />';
    echo $year . '年第' . $week . '周结束日期:' . date("Y-m-d", $timestamp['end']);
    ?>

    代码二:

    <?php
    header("Content-type:text/html;charset=utf-8");
    function getIsoWeeksInYear($year)
    {
     $date = new DateTime;
     $date->setISODate($year, 53);
     return ($date->format("W") === "53" ? 53 : 52);
    }
    function weekday($custom_date)
    {
     $week_start = date('d-m-Y', strtotime('this week monday', $custom_date));
     $week_end = date('d-m-Y', strtotime('this week sunday', $custom_date));
     $week_array[0] = $week_start;
     $week_array[1] = $week_end;
     return $week_array;
    }
    echo '<br> Weeks in 2013<br>' . getIsoWeeksInYear(2013);
    $weekday = weekday(strtotime(date('d-m-Y', strtotime('5-8-2013'))));
    echo '<br> 10-8-2013';
    echo '<br>Start: ' . $weekday[0];
    echo '<br>End: ' . $weekday[1];
    ?>
    2019-07-17 18:56:18
    赞同 展开评论 打赏
问答分类:
PHP
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-深入浅出网络编程与swoole内核-吴镇宇 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载