douzhe9075 2015-05-29 12:57
浏览 14
已采纳

在包含星期的字符串之间获取正确的日期

So I've got this string '5/25/15-5/29/15', and a page that loads a table with monday-friday. I would ideally like to put each day in the table.

For example:

Monday - Tuesday - Wednesday - Thursday - Friday
 5/25  -  5/26   -   5/27    -   5/28   -  5/29

How can I accomplish this in php/html? Or javascript etc.

update: I need this to be universal to any date string that I give it, each date will be the same format. But it has to work for the string '6/29/15-7/3/15' as well.

edit: Also, not sure why I got downvotes on this. Maybe the answer is more clear than I can see. Sorry about that.

  • 写回答

1条回答 默认 最新

  • dongyuan2388 2015-05-29 13:16
    关注

    In the comment you say you need to pull the dates from the string, using the DateTime class and some of its methods will help you do that (In $string you can put any range.)

    <?php
    $string = '5/25/15-5/29/15';
    $datesExpl = explode('-', $string);
    
    $date1 = new DateTime($datesExpl[0]);
    $date2 = new DateTime($datesExpl[1]);
    $interval = $date1->diff($date2);
    
    echo $date1->format('d-m-Y')."
    "; 
    for($i = 1; $i <= $interval->days; $i++){
        $nDate = $date1->modify('+1 day');
        echo $nDate->format('d-m-Y')."
    ";
    }
    
    /* 
    RETURNS:
    25-05-2015
    26-05-2015
    27-05-2015
    28-05-2015
    29-05-2015 
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题