使用MYSQL& PHP [英] Converting day of week to dates for the current month with MYSQL & PHP

查看:183
本文介绍了使用MYSQL& PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了一阵子搜索,找不到任何答案。真的很简单的问题我希望。



在我的数据库中我有一个字符串列dayOfWeek。



我使用它来跟踪每周周日活动的日期。通常情况下,我将使用PHP与当天的dayOfWeek相匹配,并显示正确的事件。



我想做的是在将来显示一个日历,其中列出了每周经常性事件的未来日期。



例如,如果事件发生在星期二,而2017年2月有7日,14日,21日和28日的星期二。我想使用PHP来显示。



我知道strtotime()用于做相反的事情,从日期开始一天,但可以相反要轻松完成?



对不起,如果我的问题措辞不好或缺少信息。

解决方案

用于FEB 2017 TUESDAYS的用例

 code> $ n = date('t',strtotime(2017-02-01)); //找到这个月没有天数
$ dates = array(); ($ i = 1; $ i $ = $ n; $ i ++){
$ day = date(D,strtotime(2017-02 - $ i));
// find weekdays

if($ day ==Tue){
$ dates [] =2017-02 - $ i;
}
}
print_r($ dates);

http://phpfiddle.org/main/code/s8wq-xx44


Spent a while searching and couldn't find any answers. Really simple question I hope.

In my database I have a string column dayOfWeek.

I use this to track what day of week weekly events are scheduled for. Usually I match the dayOfWeek to the current day using PHP and display the correct events.

What I want to do is show a calendar into the future which makes a list of the future dates of the weekly recurring event.

For example, if the event takes place on Tuesday, and Feb 2017 has Tuesdays on the 7th, 14th, 21st, and 28th. I'd like to use PHP to display as such.

I know strtotime() is used to do the opposite, find a day from the date, but can the opposite be done easily?

Sorry if my question is poorly worded or missing information.

解决方案

USE CASE FOR FEB 2017 TUESDAYS

$n=date('t',strtotime("2017-02-01")); // find no of days in this month
$dates=array();
for ($i=1;$i<=$n;$i++){
     $day=date("D",strtotime("2017-02-".$i)); //find weekdays

    if($day=="Tue"){
        $dates[]="2017-02-".$i;
    }
}
print_r($dates);

http://phpfiddle.org/main/code/s8wq-xx44

这篇关于使用MYSQL&amp; PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆