修改 PHP 数组中的日期
Modifying dates in a PHP array
我可以用接下来 4 周每个星期一的日期填充一个数组。问题是当周一凌晨 12 点到来时,它会立即将日期提前一周。我不希望它在星期一结束之前这样做,比如星期二凌晨 12 点。有什么想法吗?
$start = strtotime( "next monday" );
$end = strtotime( "+4 weeks", $start );
while ( $start < $end ) {
$dates[] = date( "D, M d", $start );
$start = strtotime( "+1 week", $start );
}
也许我可以离开数组,当我回显它们时改变它?这就是我现在正在做的。
<h3><?php echo $dates[0]; ?></h3>
<p></p>
<h3><?php echo $dates[1]; ?></h3>
<p></p>
等等
当我必须处理日期计算时,我使用 Carbon。这是一个不错的选择! https://github.com/briannesbitt/Carbon
您可以设置开始日期并开始计算。酷!
希望对您有所帮助!
今天倒退 1 天
$start = strtotime( "-1 day" );
$start = strtotime( "next monday", $start );
正如您所指出的,问题是 strtotime("next monday")
returns 本周的星期一 星期日但 下星期一 周日之后。
看来你总是想要这周的星期一。因此,您可以使用相对时间字符串 monday this week:
$start = strtotime('monday this week');
我明白了。我刚刚在 while 之前添加了一个 if 语句。如果当天是星期一,它只是将一周的开始日期移到 "past" 中,使其等于今天的日期。全部内容如下:
$start == strtotime( 'next monday' );
if( date('N') == 1 ) {
$start = strtotime( '-1 week', $start );
}
$end = strtotime( '+4 weeks', $start );
while ( $start < $end ) {
$dates[] = date( 'D, M d', $start );
$start = strtotime( '+1 week', $start );
}
我可以用接下来 4 周每个星期一的日期填充一个数组。问题是当周一凌晨 12 点到来时,它会立即将日期提前一周。我不希望它在星期一结束之前这样做,比如星期二凌晨 12 点。有什么想法吗?
$start = strtotime( "next monday" );
$end = strtotime( "+4 weeks", $start );
while ( $start < $end ) {
$dates[] = date( "D, M d", $start );
$start = strtotime( "+1 week", $start );
}
也许我可以离开数组,当我回显它们时改变它?这就是我现在正在做的。
<h3><?php echo $dates[0]; ?></h3>
<p></p>
<h3><?php echo $dates[1]; ?></h3>
<p></p>
等等
当我必须处理日期计算时,我使用 Carbon。这是一个不错的选择! https://github.com/briannesbitt/Carbon
您可以设置开始日期并开始计算。酷!
希望对您有所帮助!
今天倒退 1 天
$start = strtotime( "-1 day" );
$start = strtotime( "next monday", $start );
正如您所指出的,问题是 strtotime("next monday")
returns 本周的星期一 星期日但 下星期一 周日之后。
看来你总是想要这周的星期一。因此,您可以使用相对时间字符串 monday this week:
$start = strtotime('monday this week');
我明白了。我刚刚在 while 之前添加了一个 if 语句。如果当天是星期一,它只是将一周的开始日期移到 "past" 中,使其等于今天的日期。全部内容如下:
$start == strtotime( 'next monday' );
if( date('N') == 1 ) {
$start = strtotime( '-1 week', $start );
}
$end = strtotime( '+4 weeks', $start );
while ( $start < $end ) {
$dates[] = date( 'D, M d', $start );
$start = strtotime( '+1 week', $start );
}