Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何获得最后一个”;特别(三月除外)“;PHP中的月份?_Php - Fatal编程技术网

“如何获得最后一个”;特别(三月除外)“;PHP中的月份?

“如何获得最后一个”;特别(三月除外)“;PHP中的月份?,php,Php,我想展示上一个三月。我正在使用以下代码 date("Y-m-d",strtotime("last March")); 任何建议或参考都会非常有用。请参阅 或 或 或者,如果你有约会的话 $mydate = "2010-05-12 13:57:01"; 你可以这样 $month = date("m",strtotime($mydate)); m表示月份的数字,前导零为01到12 $year = date('Y'); if (date('n') < 3) { $year--;

我想展示上一个三月。我正在使用以下代码

 date("Y-m-d",strtotime("last March"));
任何建议或参考都会非常有用。

请参阅

或者,如果你有约会的话

$mydate = "2010-05-12 13:57:01";
你可以这样

$month = date("m",strtotime($mydate));
m表示月份的数字,前导零为01到12

$year = date('Y');
if (date('n') < 3) {
    $year--;
}

$beginningOfLastMarch = mktime(0, 0, 0, 3, 1, $year);
n表示月份的数字,不带前导零1到12

$year=date('Y');
$year = date('Y');
if (date('n') < 3) {
    $year--;
}

$beginningOfLastMarch = mktime(0, 0, 0, 3, 1, $year);
如果(日期('n')<3){ 美元年--; } $BEGININGOFLASTMARCH=mktime(0,0,0,3,1,$year);
我找到了一个更好的方法,但将我的旧解决方案放在了底部,因为它在某些用例中可能仍然适用于某些人

我找到了一个更好的解决方案,将日期预先设置为strotime,然后使用相对的选择器。假设我想选择去年3月4日的
。我只是:

strtotime('{THIS_YEAR}-03-04 last year');
显然,在处理这个字符串之前,我会将
{this_YEAR}
替换为
date('Y')
。这是可行的,因为我硬编码了我想要的值<代码>03-04为3月4日。我可以用我喜欢的任何日期替换这些数字

老办法
我找到了一个适合我的解决方案,它实际上并不涉及编写任何复杂的算法或任何东西。不过,它确实需要稍微扩展strotime的语法

尽管PHP的strotime并不是完美的(对于所有用例),但我发现,如果将strotime连接在一起,那么就可以使它非常强大

比如说,如果我想选择上个月的第四个
,我真的做不到
strotime
实际上不接受序数。即
4th

然而,我可以做上个月的第一天,非常接近。我所需要的就是改变这一天

strotime
允许将
time()
作为第二个参数传递。这意味着您可以像这样链接strotime

$time = time();
$time = strtotime('first day of last month', $time);
$time = strtotime('+3 days', $time);
echo date('Y-m-d H:i', $time);

// I know we don't really need the initial `$time = time()`. It is there for clarity
这将给我们提供正确的时间:
上月4日

这是一个小的语法糖进来

我们可以编写一个函数,它接受由delimeter分隔的部分
strotime
字符串:

function my_strtotime($str, $time = null, $delim = '|'){
    $time = ($time==null ? time() : $time);
    foreach(explode($delim, $str) as $cmd){
        $time = strtotime($cmd, $time);
    }
    return $time;
}
可以这样使用:

$str = 'first day of last month | +3 days';
echo date('Y-m-d H:i', my_strtotime($str));
就在这里<代码>上个月的第4天

不需要从
explode
调用中去除空白,因为
strotime
已经处理了额外的空白

这可以处理复杂的时间间隔,如去年的
3月1日+9天| 14:00
,它将始终在去年3月10日下午2点返回

最好的一点是,可以使用所需的值生成字符串,如
去年| 3月1日|+9天| 14:00
,例如

last year | first day of {MONTH} | +{DAYS-1} days | {TIME}

这可能需要额外的工作来改进它,但我只是想快速地将其输出到这里,以便帮助其他人解决这个问题

不,它将我的输出作为“1970-01-01”……您的服务器上的日期是否正确?下没有定义“去年3月”。是的,我得到了正确的“日期(“Y-m-d”,strotime(“上周一”);”但就月份而言,它不起作用了去年三月。。前进你是想得到去年三月的最后一天还是什么?我认为获取去年3月的
日期没有任何意义,但是
上周一
非常有意义,因为您获取的是最后一个星期一的日期。问题似乎是如何生成相对月份。他也可以这样获得$dtA=new DateTime()$日期=$dtA->格式(“Y-m-d h:i:s”)$newDate=strottime('-1个月',strottime($date));它是有用的,但它给出的输出是当前月份的输出;从前面的代码继续。。。这将正确地给出输出。。
$str = 'first day of last month | +3 days';
echo date('Y-m-d H:i', my_strtotime($str));
last year | first day of {MONTH} | +{DAYS-1} days | {TIME}