strtotime;选择特定的日子;星期四(今天)不上班



我以为我的工作做得很好,然后意识到今天是一个特别的月份,它变得复杂了。

这是我的:

<!-- determines first Wednesday and first Thursday of the month to echo something different than the rest of the days of the month. -->
<?php
$firstwed = strtotime("first Wednesday, first Thursday". date("F Y"));
$now = strtotime('today'); 
if( $firstwed == $now) {
  echo "Registration is closed until our next event!";
  // do something
} else {
  echo 'REGISTER AND PAY ONLINE HERE';
  // do something else
}
?>

但我昨天也需要它的目标(周三,但碰巧是上个月)。

如果我只瞄准今天(星期四),它甚至似乎都不起作用。??

基本上,故事是这样的,每个月的第一个星期五,他们有一个会议,需要在周三和周四之前登记结束;然后在周五重新开放。

请帮忙,非常感谢!

编辑:还有谁能在这个问题上提供帮助吗?我还是没有任何进展。

你的版本实际上是在比较第二个星期四:

php > echo date('c', strtotime('first wednesday, first thursday'));
2011-12-08T00:00:00-06:00   <--- dec 8th = thursday, but not **THE** first thursday
php > echo date('c', strtotime('first wednesday'));
2011-12-07T00:00:00-06:00   <--- dec 7th = wednesday
php > echo date('c', strtotime('today'));
2011-12-01T00:00:00-06:00   <-- dec 1st = thursday <-- actual first thursday of the emonth

strtotime很擅长猜测你想要什么,但它不是绝对正确的,这是它在你面前爆炸的情况之一。它查找的是今天之后的"第一个星期四",而不是当月的第一个星期四。

php > echo date('c', strtotime('first thursday december'));
2011-12-08T00:00:00-06:00
php > echo date('c', strtotime('thursday december'));
2011-12-01T00:00:00-06:00
php > echo date('c', strtotime('thursday'));
2011-12-01T00:00:00-06:00

不是特别直观。

您最好使用正式的DateTime调用来获取当前月的实际第一天的"星期几",并查看它是否是星期三/星期四。

你的问题很难理解,
如果我理解正确的话,
以下是经过一些简化的增强版本:-

$today = explode(',', date('d,D,t'));  // month day, week day, days in month
switch ($today[1])
{
  case "Wed":
    if (($today[0]+2 > $today[2]) || $today[0] <=7)
    {
      //closed
    }
  case "Thu":
    if (($today[0]+1 > $today[2]) || $today[0] <=7)
    {
      //closed
    }
    break;
}

因为无论如何,第一个星期三/星期四的月日永远不会超过7

最新更新