如何找到当前日期在PHP月份 [英] How to find current day no. in month in php
本文介绍了如何找到当前日期在PHP月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天的日期是2014年1月27日,所以我使用以下函数获得了日期名称:
Today's date is 27-01-2014 so I got day name using following function:
$t=date('d-m-Y');
$day = strtolower(date("D",strtotime($t)));
现在,日期名称为 mon
。
如何找到这个星期一是本月的第四个星期一?
换句话说,我要查找一个月中特定日期(例如星期一)的1日,2日,3日,4日?
How to find that this Monday is the forth Monday of current month? In other words, I am trying to find the 1st, 2nd, 3rd, 4th of a particular day (eg. Monday) of a month?
推荐答案
数学部分的学分归Jon(上)
Credit for the Math part goes to Jon (above)
结合您的代码,可以按以下方式实现完整的解决方案
In combination with your code, full solution can be implemented as follows
$t=date('d-m-Y');
$dayName = strtolower(date("D",strtotime($t)));
$dayNum = strtolower(date("d",strtotime($t)));
echo floor(($dayNum - 1) / 7) + 1
否则为具有可选日期的函数
or else as a function with optional date
PHP Fiddle 此处
PHP Fiddle here
这只是返回您所请求的数字。
This just return the number you are requesting.
function dayNumber($date=''){
if($date==''){
$t=date('d-m-Y');
} else {
$t=date('d-m-Y',strtotime($date));
}
$dayName = strtolower(date("D",strtotime($t)));
$dayNum = strtolower(date("d",strtotime($t)));
$return = floor(($dayNum - 1) / 7) + 1;
return $return;
}
echo dayNumber('2014-01-27');
这篇关于如何找到当前日期在PHP月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文