开发者

strtotime("next march")

Going thro开发者_开发问答ugh a weird problem here.

I am trying to get the date of coming March, but it doesn't seem to work.

$nextMarch = strtotime("next march");

What could be wrong with this tiny little code?

This is a question related to this question:

Timestamp of nearest valid month


I think you'll have to give strtotime some other (and/or additional) information.

For instance, the number of the day you want in march :

$nextMarch = strtotime("1 march");
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));

But you'll get march this year :

int 1235862000
string '2009-03-01 00:00:00' (length=19)

So, to get "next" march, i.e., next year :

$nextMarch = strtotime("1 march +1 year");
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));

And you get :

int 1267398000
string '2010-03-01 00:00:00' (length=19)

So, maybe you'll have to do some calculation by yourself, to know whether you want this year or the next one. Something like this might do the trick:

$nextMarch = strtotime("1 march");
if ($nextMarch < time()) {
  $nextMarch = strtotime("1 march +1 year");
}
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));

(I don't really like this idea, but it seems to be working -- even though a simpler solution would definitely be nice...)


Using strtotime here seems a bit odd and weird for me. I'd prefer using mktime for this.

mktime (0,0,0,3,1,date("Y")+1);


strtotime() is not that smart. You can only do weekdays with next or last.


I'd be inclined to do more mangling myself and rely less on strtotime:

$thisYear = date("Y");
$nextYear = $thisYear + 1;
$nextMarch = date($yourFormat, strtotime("March 1, $nextYear"));


What do you mean 'date of coming March'? March is a collection of 31 dates (one for each day).

Anyway, this example of strtotime() (from the online Manual) uses 'next month', to return "the last day of the current month"

It might be worth a look.


I had to do something similar on another project, here's what did:

function getNextMarch($timestamp) {



$fields = array();
$fields['timestamp'] = $timestamp;
$fields['unixtime'] = strtotime($timestamp);
$fields['current_month'] = date("m", $fields['unixtime']);
$fields['end_date_pre'] = new DateTime($fields['timestamp']);

switch ($fields['current_month']) {
    case 4 :
        $return = 11;
        break;
    case 5 :
        $return = 10;
        break;
    case 6 :
        $return = 9;
        break;
    case 7 :
        $return = 8;
        break;
    case 8 :
        $return = 7;
        break;
    case 9 :
        $return = 6;
        break;
    case 10 :
        $return = 5;
        break;
    case 11 :
        $return = 4;
        break;
    case 12 :
        $return = 3;
        break;
    case 1 :
        $return = 2;
        break;
    case 2 :
        $return = 1;
        break;
    case 3 :
        $return = 0;
        break;
}
$fields['str'] = $return . ' months';
if ($return > 1) {
    $fields['str'] = $return . ' month';
}

date_add($fields['end_date_pre'], date_interval_create_from_date_string($fields['str']));
return $fields['end_date_post'] = $fields['end_date_pre']->format('Y-m-d G:i:s');

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜